From be00693ebc1a944b49c4b8d162b93bd5112ab037 Mon Sep 17 00:00:00 2001 From: Thorsten von Eicken Date: Sun, 16 Feb 2020 16:23:55 -0800 Subject: [PATCH 1/6] proof of concept for esp32 stubs to call platform-dependent functions --- examples/natmod/esp-idf/Makefile | 32 + examples/natmod/esp-idf/counter.c | 57 ++ examples/natmod/esp-idf/dyn_esp32.mk | 944 +++++++++++++++++++++++++++ ports/esp32/Makefile | 14 +- ports/esp32/plat_relo.py | 101 +++ py/dynruntime.mk | 5 + py/nativeglue.c | 3 + py/nativeglue.h | 1 + tools/mpy_ld.py | 1 + 9 files changed, 1156 insertions(+), 2 deletions(-) create mode 100644 examples/natmod/esp-idf/Makefile create mode 100644 examples/natmod/esp-idf/counter.c create mode 100644 examples/natmod/esp-idf/dyn_esp32.mk create mode 100755 ports/esp32/plat_relo.py diff --git a/examples/natmod/esp-idf/Makefile b/examples/natmod/esp-idf/Makefile new file mode 100644 index 0000000000000..2874ce5be95f2 --- /dev/null +++ b/examples/natmod/esp-idf/Makefile @@ -0,0 +1,32 @@ +# Location of top-level MicroPython directory +MPY_DIR = ../../.. + +# Espressif ESP-IDF path +IDF_PATH := /home/src/esp32/esp-idf-micropython +# Board to get correct ESP-IDF config +BOARD := GENERIC +# xtensa toolchain bin dir +PATH := $(IDF_PATH)/xtensa-esp32-elf/bin:$(PATH) + +# Name of module +MOD = counter + +# Source files (.c or .py) +SRC = counter.c + +# Architecture to build for (x86, x64, armv7m, xtensa, xtensawin) +ARCH = xtensawin + +# Include to get the rules for compiling and linking the module +include dyn_esp32.mk +include $(MPY_DIR)/py/dynruntime.mk + +#plat_stubs.c: $(MPY_DIR)/ports/esp32/plat_relo.py +# $(ECHO) "GEN $@" +# $(Q)mkdir -p build +# $(Q)$< -c >$@ + +build/plat_stubs.s: $(MPY_DIR)/ports/esp32/plat_relo.py + $(ECHO) "GEN $@" + $(Q)mkdir -p build + $(Q)$< >$@ diff --git a/examples/natmod/esp-idf/counter.c b/examples/natmod/esp-idf/counter.c new file mode 100644 index 0000000000000..fdc6637b22ac6 --- /dev/null +++ b/examples/natmod/esp-idf/counter.c @@ -0,0 +1,57 @@ +/* This example demonstrates using an ESP32 esp-idf function +*/ + +// Include the header file to get access to the MicroPython API +#include "py/dynruntime.h" + +// ESP-IDF imports +#include +#include + +// This is the function which will be called from Python as counter_init(x) +STATIC mp_obj_t counter_init(mp_obj_t x_obj) { + // Extract the integer from the MicroPython input object + mp_int_t x = mp_obj_get_int(x_obj); + // Call pcnt_unit_config + pcnt_config_t cfg = { + .unit = 0, + .channel = 0, + .pulse_gpio_num = x, + .ctrl_gpio_num = -1, // not yet supported + .counter_h_lim = 0, + .counter_l_lim = 0, + .pos_mode = PCNT_COUNT_DIS, // positive edge is no-op + .neg_mode = PCNT_COUNT_DIS, // negative edge is no-op + .lctrl_mode = PCNT_MODE_KEEP, // ctrl pin is no-op + .hctrl_mode = PCNT_MODE_KEEP, // ctrl pin is no-op + }; + esp_err_t err = pcnt_unit_config(&cfg); + // Convert the result to a MicroPython integer object and return it + return mp_obj_new_int(err); +} +// Define a Python reference to the function above +STATIC MP_DEFINE_CONST_FUN_OBJ_1(counter_init_obj, counter_init); + +STATIC mp_obj_t mem_info() { + extern int mp_micropython_mem_info(int, int); + mp_printf(&mp_plat_print, "mp_fun_table is at 0x%x\n", &mp_fun_table); + mp_printf(&mp_plat_print, "plat_relo_tab is at 0x%x\n", mp_fun_table.plat_relo_tab); + mp_printf(&mp_plat_print, "calling mp_micropython_mem_info\n"); + int i = mp_micropython_mem_info(0, 0); + mp_printf(&mp_plat_print, "mp_micropython_mem_info: %d/0x%x\n", i, i); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_0(mem_info_obj, mem_info); + +// This is the entry point and is called when the module is imported +mp_obj_t mpy_init(mp_obj_fun_bc_t *self, size_t n_args, size_t n_kw, mp_obj_t *args) { + // This must be first, it sets up the globals dict and other things + MP_DYNRUNTIME_INIT_ENTRY + + // Make the function available in the module's namespace + mp_store_global(MP_QSTR_counter_init, MP_OBJ_FROM_PTR(&counter_init_obj)); + mp_store_global(MP_QSTR_mem_info, MP_OBJ_FROM_PTR(&mem_info_obj)); + + // This must be last, it restores the globals dict + MP_DYNRUNTIME_INIT_EXIT +} diff --git a/examples/natmod/esp-idf/dyn_esp32.mk b/examples/natmod/esp-idf/dyn_esp32.mk new file mode 100644 index 0000000000000..b03fd2407f258 --- /dev/null +++ b/examples/natmod/esp-idf/dyn_esp32.mk @@ -0,0 +1,944 @@ +# Select the board to build for: if not given on the command line, +# then default to GENERIC. +BOARD ?= GENERIC + +PORT_DIR := $(MPY_DIR)/ports/esp32 + +# If the build directory is not given, make it reflect the board name. +BUILD ?= build + +BOARD_DIR ?= $(PORT_DIR)/boards/$(BOARD) +BOARD_BUILD ?= $(PORT_DIR)/build-$(BOARD) +#ifeq ($(wildcard $(BOARD_DIR)/.),) +#$(error Invalid BOARD specified: $(BOARD_DIR)) +#endif +# +#include ../../py/mkenv.mk +# +## Optional (not currently used for ESP32) +#-include mpconfigport.mk +# +#ifneq ($(SDKCONFIG),) +#$(error Use the BOARD variable instead of SDKCONFIG) +#endif + +# Expected to set SDKCONFIG +include $(BOARD_DIR)/mpconfigboard.mk +SDKCONFIG := $(addprefix $(PORT_DIR)/,$(SDKCONFIG)) + +## qstr definitions (must come before including py.mk) +#QSTR_DEFS = qstrdefsport.h +#QSTR_GLOBAL_DEPENDENCIES = $(BOARD_DIR)/mpconfigboard.h +#QSTR_GLOBAL_REQUIREMENTS = $(SDKCONFIG_H) + +#MICROPY_PY_USSL = 0 +#MICROPY_SSL_AXTLS = 0 +#MICROPY_PY_BTREE = 1 +#MICROPY_VFS_FAT = 1 +#MICROPY_VFS_LFS2 = 1 + +#FROZEN_MANIFEST ?= boards/manifest.py + +## include py core make definitions +#include $(TOP)/py/py.mk + +#GIT_SUBMODULES = lib/berkeley-db-1.xx + +#PORT ?= /dev/ttyUSB0 +#BAUD ?= 460800 +#FLASH_MODE ?= dio +#FLASH_FREQ ?= 40m +#FLASH_SIZE ?= 4MB +#CROSS_COMPILE ?= xtensa-esp32-elf- +#OBJDUMP = $(CROSS_COMPILE)objdump + +#SDKCONFIG_COMBINED = $(BUILD)/sdkconfig.combined +#SDKCONFIG_H = $(BOARD_BUILD)/sdkconfig.h + +# the git hash of the currently supported ESP IDF version +ESPIDF_SUPHASH_V3 := 6ccb4cf5b7d1fdddb8c2492f9cbc926abaf230df +ESPIDF_SUPHASH_V4 := 310beae373446ceb9a4ad9b36b5428d7fdf2705f + +define print_supported_git_hash +$(info Supported git hash (v3.3): $(ESPIDF_SUPHASH_V3)) +$(info Supported git hash (v4.0-beta1) (experimental): $(ESPIDF_SUPHASH_V4)) +endef + +# paths to ESP IDF and its components +ifeq ($(ESPIDF),) +ifneq ($(IDF_PATH),) +ESPIDF = $(IDF_PATH) +else +$(info The ESPIDF variable has not been set, please set it to the root of the esp-idf repository.) +$(info See README.md for installation instructions.) +$(call print_supported_git_hash) +$(error ESPIDF not set) +endif +endif + +ESPCOMP = $(ESPIDF)/components +#ESPTOOL ?= $(ESPCOMP)/esptool_py/esptool/esptool.py +ESPCOMP_KCONFIGS = $(shell find $(ESPCOMP) -name Kconfig) +ESPCOMP_KCONFIGS_PROJBUILD = $(shell find $(ESPCOMP) -name Kconfig.projbuild) + +# verify the ESP IDF version +ESPIDF_CURHASH := $(shell git -C $(ESPIDF) show -s --pretty=format:'%H') + +ifeq ($(ESPIDF_CURHASH),$(ESPIDF_SUPHASH_V3)) +$(info Building with ESP IDF v3) +else ifeq ($(ESPIDF_CURHASH),$(ESPIDF_SUPHASH_V4)) +$(info Building with ESP IDF v4) + +PYPARSING_VERSION = $(shell python3 -c 'import pyparsing; print(pyparsing.__version__)') +ifneq ($(PYPARSING_VERSION),2.3.1) +$(info ** ERROR **) +$(info EDP IDF requires pyparsing version less than 2.4) +$(info You will need to set up a Python virtual environment with pyparsing 2.3.1) +$(info Please see README.md for more information) +$(error Incorrect pyparsing version) +endif +else +$(info ** WARNING **) +$(info The git hash of ESP IDF does not match the supported version) +$(info The build may complete and the firmware may work but it is not guaranteed) +$(info ESP IDF path: $(ESPIDF)) +$(info Current git hash: $(ESPIDF_CURHASH)) +$(call print_supported_git_hash) +endif + +# pretty format of ESP IDF version, used internally by the IDF +IDF_VER := $(shell git -C $(ESPIDF) describe) + +#ifeq ($(shell which $(CC) 2> /dev/null),) +#$(info ** ERROR **) +#$(info Cannot find C compiler $(CC)) +#$(info Add the xtensa toolchain to your PATH. See README.md) +#$(error C compiler missing) +#endif + +## Support BLE by default when building with IDF 4.x. +## Can be explicitly disabled on the command line or board config. +#ifeq ($(ESPIDF_CURHASH),$(ESPIDF_SUPHASH_V4)) +#MICROPY_PY_BLUETOOTH ?= 1 +#ifeq ($(MICROPY_PY_BLUETOOTH),1) +#SDKCONFIG += boards/sdkconfig.ble +#MICROPY_BLUETOOTH_NIMBLE = 1 +#endif +#endif +# +# include sdkconfig to get needed configuration values +include $(SDKCONFIG) +$(shell cp $(BOARD_BUILD)/sdkconfig* $(BUILD)) + +################################################################################ +# Compiler and linker flags + +INC_ESPCOMP += -I$(PORT_DIR) +#INC += -I$(TOP) +#INC += -I$(TOP)/lib/mp-readline +#INC += -I$(TOP)/lib/netutils +#INC += -I$(TOP)/lib/timeutils +INC_ESPCOMP += -I$(BUILD) + +INC_ESPCOMP += -I$(ESPCOMP)/bootloader_support/include +INC_ESPCOMP += -I$(ESPCOMP)/bootloader_support/include_bootloader +INC_ESPCOMP += -I$(ESPCOMP)/console +INC_ESPCOMP += -I$(ESPCOMP)/driver/include +INC_ESPCOMP += -I$(ESPCOMP)/driver/include/driver +INC_ESPCOMP += -I$(ESPCOMP)/efuse/include +INC_ESPCOMP += -I$(ESPCOMP)/efuse/esp32/include +INC_ESPCOMP += -I$(ESPCOMP)/esp32/include +INC_ESPCOMP += -I$(ESPCOMP)/espcoredump/include +INC_ESPCOMP += -I$(ESPCOMP)/soc/include +INC_ESPCOMP += -I$(ESPCOMP)/soc/esp32/include +INC_ESPCOMP += -I$(ESPCOMP)/heap/include +INC_ESPCOMP += -I$(ESPCOMP)/log/include +INC_ESPCOMP += -I$(ESPCOMP)/newlib/platform_include +INC_ESPCOMP += -I$(ESPCOMP)/newlib/include +INC_ESPCOMP += -I$(ESPCOMP)/nvs_flash/include +INC_ESPCOMP += -I$(ESPCOMP)/freertos/include +INC_ESPCOMP += -I$(ESPCOMP)/esp_ringbuf/include +INC_ESPCOMP += -I$(ESPCOMP)/esp_event/include +INC_ESPCOMP += -I$(ESPCOMP)/tcpip_adapter/include +INC_ESPCOMP += -I$(ESPCOMP)/lwip/lwip/src/include +INC_ESPCOMP += -I$(ESPCOMP)/lwip/port/esp32/include +INC_ESPCOMP += -I$(ESPCOMP)/lwip/include/apps +INC_ESPCOMP += -I$(ESPCOMP)/mbedtls/mbedtls/include +INC_ESPCOMP += -I$(ESPCOMP)/mbedtls/port/include +INC_ESPCOMP += -I$(ESPCOMP)/mdns/include +INC_ESPCOMP += -I$(ESPCOMP)/mdns/private_include +INC_ESPCOMP += -I$(ESPCOMP)/spi_flash/include +INC_ESPCOMP += -I$(ESPCOMP)/ulp/include +INC_ESPCOMP += -I$(ESPCOMP)/vfs/include +INC_ESPCOMP += -I$(ESPCOMP)/xtensa-debug-module/include +INC_ESPCOMP += -I$(ESPCOMP)/wpa_supplicant/include +INC_ESPCOMP += -I$(ESPCOMP)/wpa_supplicant/port/include +INC_ESPCOMP += -I$(ESPCOMP)/app_trace/include +INC_ESPCOMP += -I$(ESPCOMP)/app_update/include +INC_ESPCOMP += -I$(ESPCOMP)/pthread/include +INC_ESPCOMP += -I$(ESPCOMP)/smartconfig_ack/include +INC_ESPCOMP += -I$(ESPCOMP)/sdmmc/include + +ifeq ($(ESPIDF_CURHASH),$(ESPIDF_SUPHASH_V4)) +INC_ESPCOMP += -I$(ESPCOMP)/esp_common/include +INC_ESPCOMP += -I$(ESPCOMP)/esp_eth/include +INC_ESPCOMP += -I$(ESPCOMP)/esp_event/private_include +INC_ESPCOMP += -I$(ESPCOMP)/esp_rom/include +INC_ESPCOMP += -I$(ESPCOMP)/esp_wifi/include +INC_ESPCOMP += -I$(ESPCOMP)/esp_wifi/esp32/include +INC_ESPCOMP += -I$(ESPCOMP)/lwip/include/apps/sntp +INC_ESPCOMP += -I$(ESPCOMP)/spi_flash/private_include +INC_ESPCOMP += -I$(ESPCOMP)/wpa_supplicant/include/esp_supplicant +INC_ESPCOMP += -I$(ESPCOMP)/xtensa/include +INC_ESPCOMP += -I$(ESPCOMP)/xtensa/esp32/include +ifeq ($(CONFIG_BT_NIMBLE_ENABLED),y) +INC_ESPCOMP += -I$(ESPCOMP)/bt/include +INC_ESPCOMP += -I$(ESPCOMP)/bt/common/osi/include +INC_ESPCOMP += -I$(ESPCOMP)/bt/common/btc/include +INC_ESPCOMP += -I$(ESPCOMP)/bt/common/include +INC_ESPCOMP += -I$(ESPCOMP)/bt/host/nimble/nimble/porting/nimble/include +INC_ESPCOMP += -I$(ESPCOMP)/bt/host/nimble/port/include +INC_ESPCOMP += -I$(ESPCOMP)/bt/host/nimble/nimble/nimble/include +INC_ESPCOMP += -I$(ESPCOMP)/bt/host/nimble/nimble/nimble/host/include +INC_ESPCOMP += -I$(ESPCOMP)/bt/host/nimble/nimble/nimble/host/services/ans/include +INC_ESPCOMP += -I$(ESPCOMP)/bt/host/nimble/nimble/nimble/host/services/bas/include +INC_ESPCOMP += -I$(ESPCOMP)/bt/host/nimble/nimble/nimble/host/services/gap/include +INC_ESPCOMP += -I$(ESPCOMP)/bt/host/nimble/nimble/nimble/host/services/gatt/include +INC_ESPCOMP += -I$(ESPCOMP)/bt/host/nimble/nimble/nimble/host/services/ias/include +INC_ESPCOMP += -I$(ESPCOMP)/bt/host/nimble/nimble/nimble/host/services/lls/include +INC_ESPCOMP += -I$(ESPCOMP)/bt/host/nimble/nimble/nimble/host/services/tps/include +INC_ESPCOMP += -I$(ESPCOMP)/bt/host/nimble/nimble/nimble/host/util/include +INC_ESPCOMP += -I$(ESPCOMP)/bt/host/nimble/nimble/nimble/host/store/ram/include +INC_ESPCOMP += -I$(ESPCOMP)/bt/host/nimble/nimble/nimble/host/store/config/include +INC_ESPCOMP += -I$(ESPCOMP)/bt/host/nimble/nimble/porting/npl/freertos/include +INC_ESPCOMP += -I$(ESPCOMP)/bt/host/nimble/nimble/ext/tinycrypt/include +INC_ESPCOMP += -I$(ESPCOMP)/bt/host/nimble/esp-hci/include +endif +else +INC_ESPCOMP += -I$(ESPCOMP)/ethernet/include +INC_ESPCOMP += -I$(ESPCOMP)/expat/expat/expat/lib +INC_ESPCOMP += -I$(ESPCOMP)/expat/port/include +INC_ESPCOMP += -I$(ESPCOMP)/json/include +INC_ESPCOMP += -I$(ESPCOMP)/json/port/include +INC_ESPCOMP += -I$(ESPCOMP)/micro-ecc/micro-ecc +INC_ESPCOMP += -I$(ESPCOMP)/nghttp/port/include +INC_ESPCOMP += -I$(ESPCOMP)/nghttp/nghttp2/lib/includes +endif + +ifeq ($(ESPIDF_CURHASH),$(ESPIDF_SUPHASH_V4)) +ifeq ($(MICROPY_PY_BLUETOOTH),1) +CFLAGS_MOD += -DMICROPY_PY_BLUETOOTH=1 +CFLAGS_MOD += -DMICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE=1 + +ifeq ($(MICROPY_BLUETOOTH_NIMBLE),1) +CFLAGS_MOD += -DMICROPY_BLUETOOTH_NIMBLE=1 +endif +endif +endif + +CFLAGS = $(INC_ESPCOMP) $(CFLAGS_MOD) + +SRC_O += build/plat_stubs.o + +## these flags are common to C and C++ compilation +#CFLAGS_COMMON = -Os -ffunction-sections -fdata-sections -fstrict-volatile-bitfields \ +# -mlongcalls -nostdlib \ +# -Wall -Werror -Wno-error=unused-function -Wno-error=unused-but-set-variable \ +# -Wno-error=unused-variable -Wno-error=deprecated-declarations \ +# -DESP_PLATFORM +# +#CFLAGS_BASE = -std=gnu99 $(CFLAGS_COMMON) -DMBEDTLS_CONFIG_FILE='"mbedtls/esp_config.h"' -DHAVE_CONFIG_H +#CFLAGS = $(CFLAGS_BASE) $(INC) $(INC_ESPCOMP) +#CFLAGS += -DIDF_VER=\"$(IDF_VER)\" +#CFLAGS += $(CFLAGS_MOD) $(CFLAGS_EXTRA) +#CFLAGS += -I$(BOARD_DIR) +# +#ifeq ($(ESPIDF_CURHASH),$(ESPIDF_SUPHASH_V4)) +#CFLAGS += -DMICROPY_ESP_IDF_4=1 +#endif +# +## this is what ESPIDF uses for c++ compilation +#CXXFLAGS = -std=gnu++11 $(CFLAGS_COMMON) $(INC) $(INC_ESPCOMP) +# +#LDFLAGS = -nostdlib -Map=$(@:.elf=.map) --cref +#LDFLAGS += --gc-sections -static -EL +#LDFLAGS += -u call_user_start_cpu0 -u uxTopUsedPriority -u ld_include_panic_highint_hdl +#LDFLAGS += -u __cxa_guard_dummy # so that implementation of static guards is taken from cxx_guards.o instead of libstdc++.a +#LDFLAGS += -L$(ESPCOMP)/esp32/ld +#LDFLAGS += -L$(ESPCOMP)/esp_rom/esp32/ld +#LDFLAGS += -T $(BUILD)/esp32_out.ld +#LDFLAGS += -T $(BUILD)/esp32.project.ld +#LDFLAGS += -T esp32.rom.ld +#LDFLAGS += -T esp32.rom.libgcc.ld +#LDFLAGS += -T esp32.peripherals.ld +# +#LIBGCC_FILE_NAME = $(shell $(CC) $(CFLAGS) -print-libgcc-file-name) +#LIBSTDCXX_FILE_NAME = $(shell $(CXX) $(CXXFLAGS) -print-file-name=libstdc++.a) +# +## Debugging/Optimization +#ifeq ($(DEBUG), 1) +#CFLAGS += -g +#COPT = -O0 +#else +##CFLAGS += -fdata-sections -ffunction-sections +#COPT += -Os -DNDEBUG +##LDFLAGS += --gc-sections +#endif +# +## Options for mpy-cross +#MPY_CROSS_FLAGS += -march=xtensawin +# +## Enable SPIRAM support if CONFIG_ESP32_SPIRAM_SUPPORT=y in sdkconfig +#ifeq ($(CONFIG_ESP32_SPIRAM_SUPPORT),y) +#CFLAGS_COMMON += -mfix-esp32-psram-cache-issue +#LIBC_LIBM = $(ESPCOMP)/newlib/lib/libc-psram-workaround.a $(ESPCOMP)/newlib/lib/libm-psram-workaround.a +#else +## Additional newlib symbols that can only be used with spiram disabled. +#ifeq ($(ESPIDF_CURHASH),$(ESPIDF_SUPHASH_V4)) +#LDFLAGS += -T esp32.rom.newlib-funcs.ld +#LDFLAGS += -T esp32.rom.newlib-locale.ld +#LDFLAGS += -T esp32.rom.newlib-data.ld +#else +#LDFLAGS += -T esp32.rom.spiram_incompatible_fns.ld +#endif +#LIBC_LIBM = $(ESPCOMP)/newlib/lib/libc.a $(ESPCOMP)/newlib/lib/libm.a +#endif +# +################################################################################# +## List of MicroPython source and object files +# +#SRC_C = \ +# main.c \ +# uart.c \ +# gccollect.c \ +# mphalport.c \ +# fatfs_port.c \ +# help.c \ +# modutime.c \ +# moduos.c \ +# machine_timer.c \ +# machine_pin.c \ +# machine_touchpad.c \ +# machine_adc.c \ +# machine_dac.c \ +# machine_i2c.c \ +# machine_pwm.c \ +# machine_counter.c \ +# machine_uart.c \ +# modmachine.c \ +# modnetwork.c \ +# network_lan.c \ +# network_ppp.c \ +# nimble.c \ +# modsocket.c \ +# modesp.c \ +# esp32_partition.c \ +# esp32_rmt.c \ +# esp32_ulp.c \ +# modesp32.c \ +# espneopixel.c \ +# machine_hw_spi.c \ +# machine_wdt.c \ +# mpthreadport.c \ +# machine_rtc.c \ +# machine_sdcard.c \ +# $(wildcard $(BOARD_DIR)/*.c) \ +# $(SRC_MOD) +# +#EXTMOD_SRC_C = $(addprefix extmod/,\ +# modonewire.c \ +# modbluetooth_nimble.c \ +# ) +# +#LIB_SRC_C = $(addprefix lib/,\ +# mp-readline/readline.c \ +# netutils/netutils.c \ +# timeutils/timeutils.c \ +# utils/pyexec.c \ +# utils/interrupt_char.c \ +# utils/sys_stdio_mphal.c \ +# ) +# +#DRIVERS_SRC_C = $(addprefix drivers/,\ +# bus/softspi.c \ +# dht/dht.c \ +# ) +# +#OBJ_MP = +#OBJ_MP += $(PY_O) +#OBJ_MP += $(addprefix $(BUILD)/, $(SRC_C:.c=.o)) +#OBJ_MP += $(addprefix $(BUILD)/, $(EXTMOD_SRC_C:.c=.o)) +#OBJ_MP += $(addprefix $(BUILD)/, $(LIB_SRC_C:.c=.o)) +#OBJ_MP += $(addprefix $(BUILD)/, $(DRIVERS_SRC_C:.c=.o)) +# +## List of sources for qstr extraction +#SRC_QSTR += $(SRC_C) $(EXTMOD_SRC_C) $(LIB_SRC_C) $(DRIVERS_SRC_C) +## Append any auto-generated sources that are needed by sources listed in SRC_QSTR +#SRC_QSTR_AUTO_DEPS += +# +################################################################################# +## Generate sdkconfig.h from sdkconfig +# +#$(SDKCONFIG_COMBINED): $(SDKCONFIG) +# $(Q)$(MKDIR) -p $(dir $@) +# $(Q)$(CAT) $^ > $@ +# +#$(SDKCONFIG_H): $(SDKCONFIG_COMBINED) +# $(ECHO) "GEN $@" +# $(Q)$(MKDIR) -p $(dir $@) +# $(Q)$(PYTHON) $(ESPIDF)/tools/kconfig_new/confgen.py \ +# --output header $@ \ +# --config $< \ +# --kconfig $(ESPIDF)/Kconfig \ +# --env "IDF_TARGET=esp32" \ +# --env "IDF_CMAKE=n" \ +# --env "COMPONENT_KCONFIGS=$(ESPCOMP_KCONFIGS)" \ +# --env "COMPONENT_KCONFIGS_PROJBUILD=$(ESPCOMP_KCONFIGS_PROJBUILD)" \ +# --env "IDF_PATH=$(ESPIDF)" +# $(Q)touch $@ +# +#$(HEADER_BUILD)/qstrdefs.generated.h: $(SDKCONFIG_H) $(BOARD_DIR)/mpconfigboard.h +# +################################################################################# +## List of object files from the ESP32 IDF components +# +#ESPIDF_BOOTLOADER_SUPPORT_O = $(patsubst %.c,%.o,\ +# $(filter-out $(ESPCOMP)/bootloader_support/src/bootloader_init.c,\ +# $(wildcard $(ESPCOMP)/bootloader_support/src/*.c) \ +# $(wildcard $(ESPCOMP)/bootloader_support/src/idf/*.c) \ +# )) +# +#ESPIDF_DRIVER_O = $(patsubst %.c,%.o,$(wildcard $(ESPCOMP)/driver/*.c)) +# +#ESPIDF_EFUSE_O = $(patsubst %.c,%.o,\ +# $(wildcard $(ESPCOMP)/efuse/esp32/*.c)\ +# $(wildcard $(ESPCOMP)/efuse/src/*.c)\ +# ) +# +#$(BUILD)/$(ESPCOMP)/esp32/dport_access.o: CFLAGS += -Wno-array-bounds +#ESPIDF_ESP32_O = \ +# $(patsubst %.c,%.o,$(wildcard $(ESPCOMP)/esp32/*.c)) \ +# $(patsubst %.c,%.o,$(wildcard $(ESPCOMP)/esp32/hwcrypto/*.c)) \ +# $(patsubst %.S,%.o,$(wildcard $(ESPCOMP)/esp32/*.S)) \ +# +#ESPIDF_ESP_RINGBUF_O = $(patsubst %.c,%.o,$(wildcard $(ESPCOMP)/esp_ringbuf/*.c)) +# +#ESPIDF_HEAP_O = $(patsubst %.c,%.o,$(wildcard $(ESPCOMP)/heap/*.c)) +# +#ESPIDF_SOC_O = $(patsubst %.c,%.o,\ +# $(wildcard $(ESPCOMP)/soc/esp32/*.c) \ +# $(wildcard $(ESPCOMP)/soc/src/*.c) \ +# $(wildcard $(ESPCOMP)/soc/src/hal/*.c) \ +# ) +# +#ESPIDF_CXX_O = $(patsubst %.cpp,%.o,$(wildcard $(ESPCOMP)/cxx/*.cpp)) +# +#ESPIDF_PTHREAD_O = $(patsubst %.c,%.o,$(wildcard $(ESPCOMP)/pthread/*.c)) +# +## Assembler .S files need only basic flags, and in particular should not have +## -Os because that generates subtly different code. +## We also need custom CFLAGS for .c files because FreeRTOS has headers with +## generic names (eg queue.h) which can clash with other files in the port. +#ifeq ($(ESPIDF_CURHASH),$(ESPIDF_SUPHASH_V4)) +#CFLAGS_ASM = -I$(BUILD) -I$(ESPCOMP)/esp32/include -I$(ESPCOMP)/soc/esp32/include -I$(ESPCOMP)/freertos/include/freertos -I. -I$(ESPCOMP)/xtensa/include -I$(ESPCOMP)/xtensa/esp32/include -I$(ESPCOMP)/esp_common/include +#else +#CFLAGS_ASM = -I$(BUILD) -I$(ESPCOMP)/esp32/include -I$(ESPCOMP)/soc/esp32/include -I$(ESPCOMP)/freertos/include/freertos -I. +#endif +#$(BUILD)/$(ESPCOMP)/freertos/portasm.o: CFLAGS = $(CFLAGS_ASM) +#$(BUILD)/$(ESPCOMP)/freertos/xtensa_context.o: CFLAGS = $(CFLAGS_ASM) +#$(BUILD)/$(ESPCOMP)/freertos/xtensa_intr_asm.o: CFLAGS = $(CFLAGS_ASM) +#$(BUILD)/$(ESPCOMP)/freertos/xtensa_vectors.o: CFLAGS = $(CFLAGS_ASM) +#$(BUILD)/$(ESPCOMP)/freertos/xtensa_vector_defaults.o: CFLAGS = $(CFLAGS_ASM) +#$(BUILD)/$(ESPCOMP)/freertos/%.o: CFLAGS = $(CFLAGS_BASE) -I. -I$(BUILD) $(INC_ESPCOMP) -I$(ESPCOMP)/freertos/include/freertos -D_ESP_FREERTOS_INTERNAL +#ESPIDF_FREERTOS_O = \ +# $(patsubst %.c,%.o,$(wildcard $(ESPCOMP)/freertos/*.c)) \ +# $(patsubst %.S,%.o,$(wildcard $(ESPCOMP)/freertos/*.S)) \ +# +#ESPIDF_VFS_O = $(patsubst %.c,%.o,$(wildcard $(ESPCOMP)/vfs/*.c)) +# +#ESPIDF_LOG_O = $(patsubst %.c,%.o,$(wildcard $(ESPCOMP)/log/*.c)) +# +#ESPIDF_XTENSA_DEBUG_MODULE_O = $(patsubst %.c,%.o,$(wildcard $(ESPCOMP)/xtensa-debug-module/*.c)) +# +#ESPIDF_TCPIP_ADAPTER_O = $(patsubst %.c,%.o,$(wildcard $(ESPCOMP)/tcpip_adapter/*.c)) +# +#ESPIDF_APP_TRACE_O = $(patsubst %.c,%.o,$(wildcard $(ESPCOMP)/app_trace/*.c)) +# +#ESPIDF_APP_UPDATE_O = $(patsubst %.c,%.o,$(wildcard $(ESPCOMP)/app_update/*.c)) +# +#ESPIDF_NEWLIB_O = $(patsubst %.c,%.o,$(wildcard $(ESPCOMP)/newlib/*.c)) +# +#ESPIDF_NVS_FLASH_O = $(patsubst %.cpp,%.o,$(wildcard $(ESPCOMP)/nvs_flash/src/*.cpp)) +# +#ESPIDF_SMARTCONFIG_ACK_O = $(patsubst %.c,%.o,$(wildcard $(ESPCOMP)/smartconfig_ack/*.c)) +# +#ESPIDF_SPI_FLASH_O = $(patsubst %.c,%.o,$(wildcard $(ESPCOMP)/spi_flash/*.c)) +# +#ESPIDF_ULP_O = $(patsubst %.c,%.o,$(wildcard $(ESPCOMP)/ulp/*.c)) +# +#$(BUILD)/$(ESPCOMP)/lwip/%.o: CFLAGS += -Wno-address -Wno-unused-variable -Wno-unused-but-set-variable +#ESPIDF_LWIP_O = $(patsubst %.c,%.o,\ +# $(wildcard $(ESPCOMP)/lwip/apps/dhcpserver/*.c) \ +# $(wildcard $(ESPCOMP)/lwip/lwip/src/api/*.c) \ +# $(wildcard $(ESPCOMP)/lwip/lwip/src/apps/sntp/*.c) \ +# $(wildcard $(ESPCOMP)/lwip/lwip/src/core/*.c) \ +# $(wildcard $(ESPCOMP)/lwip/lwip/src/core/*/*.c) \ +# $(wildcard $(ESPCOMP)/lwip/lwip/src/netif/*.c) \ +# $(wildcard $(ESPCOMP)/lwip/lwip/src/netif/*/*.c) \ +# $(wildcard $(ESPCOMP)/lwip/lwip/src/netif/*/*/*.c) \ +# $(wildcard $(ESPCOMP)/lwip/port/esp32/*.c) \ +# $(wildcard $(ESPCOMP)/lwip/port/esp32/*/*.c) \ +# ) +# +#ESPIDF_MBEDTLS_O = $(patsubst %.c,%.o,\ +# $(wildcard $(ESPCOMP)/mbedtls/mbedtls/library/*.c) \ +# $(wildcard $(ESPCOMP)/mbedtls/port/*.c) \ +# $(wildcard $(ESPCOMP)/mbedtls/port/esp32/*.c) \ +# ) +# +#ESPIDF_MDNS_O = $(patsubst %.c,%.o,$(wildcard $(ESPCOMP)/mdns/*.c)) +# +#ifeq ($(ESPIDF_CURHASH),$(ESPIDF_SUPHASH_V4)) +#$(BUILD)/$(ESPCOMP)/wpa_supplicant/%.o: CFLAGS += -DESP_SUPPLICANT -DIEEE8021X_EAPOL -DEAP_PEER_METHOD -DEAP_TLS -DEAP_TTLS -DEAP_PEAP -DEAP_MSCHAPv2 -DUSE_WPA2_TASK -DCONFIG_WPS2 -DCONFIG_WPS_PIN -DUSE_WPS_TASK -DESPRESSIF_USE -DESP32_WORKAROUND -DCONFIG_ECC -D__ets__ -Wno-strict-aliasing -I$(ESPCOMP)/wpa_supplicant/src -Wno-implicit-function-declaration +#else +#$(BUILD)/$(ESPCOMP)/wpa_supplicant/%.o: CFLAGS += -DEMBEDDED_SUPP -DIEEE8021X_EAPOL -DEAP_PEER_METHOD -DEAP_MSCHAPv2 -DEAP_TTLS -DEAP_TLS -DEAP_PEAP -DUSE_WPA2_TASK -DCONFIG_WPS2 -DCONFIG_WPS_PIN -DUSE_WPS_TASK -DESPRESSIF_USE -DESP32_WORKAROUND -DALLOW_EVEN_MOD -D__ets__ -Wno-strict-aliasing +#endif +#ESPIDF_WPA_SUPPLICANT_O = $(patsubst %.c,%.o,\ +# $(wildcard $(ESPCOMP)/wpa_supplicant/port/*.c) \ +# $(wildcard $(ESPCOMP)/wpa_supplicant/src/*/*.c) \ +# ) +# +#ESPIDF_SDMMC_O = $(patsubst %.c,%.o,$(wildcard $(ESPCOMP)/sdmmc/*.c)) +# +#ifeq ($(ESPIDF_CURHASH),$(ESPIDF_SUPHASH_V4)) +#ESPIDF_ESP_COMMON_O = $(patsubst %.c,%.o,$(wildcard $(ESPCOMP)/esp_common/src/*.c)) +# +#ESPIDF_ESP_EVENT_O = $(patsubst %.c,%.o,$(wildcard $(ESPCOMP)/esp_event/*.c)) +# +#ESPIDF_ESP_WIFI_O = $(patsubst %.c,%.o,$(wildcard $(ESPCOMP)/esp_wifi/src/*.c)) +# +#ifeq ($(CONFIG_BT_NIMBLE_ENABLED),y) +#ESPIDF_BT_NIMBLE_O = $(patsubst %.c,%.o,\ +# $(wildcard $(ESPCOMP)/bt/controller/*.c) \ +# $(wildcard $(ESPCOMP)/bt/common/btc/core/*.c) \ +# $(wildcard $(ESPCOMP)/bt/common/osi/*.c) \ +# $(wildcard $(ESPCOMP)/bt/host/nimble/esp-hci/src/*.c) \ +# $(wildcard $(ESPCOMP)/bt/host/nimble/nimble/ext/tinycrypt/src/*.c) \ +# $(wildcard $(ESPCOMP)/bt/host/nimble/nimble/nimble/host/services/ans/src/*.c) \ +# $(wildcard $(ESPCOMP)/bt/host/nimble/nimble/nimble/host/services/bas/src/*.c) \ +# $(wildcard $(ESPCOMP)/bt/host/nimble/nimble/nimble/host/services/gap/src/*.c) \ +# $(wildcard $(ESPCOMP)/bt/host/nimble/nimble/nimble/host/services/gatt/src/*.c) \ +# $(wildcard $(ESPCOMP)/bt/host/nimble/nimble/nimble/host/services/ias/src/*.c) \ +# $(wildcard $(ESPCOMP)/bt/host/nimble/nimble/nimble/host/services/lls/src/*.c) \ +# $(wildcard $(ESPCOMP)/bt/host/nimble/nimble/nimble/host/services/tps/src/*.c) \ +# $(wildcard $(ESPCOMP)/bt/host/nimble/nimble/nimble/host/src/*.c) \ +# $(wildcard $(ESPCOMP)/bt/host/nimble/nimble/nimble/host/store/config/src/ble_store_config.c) \ +# $(wildcard $(ESPCOMP)/bt/host/nimble/nimble/nimble/host/store/config/src/ble_store_nvs.c) \ +# $(wildcard $(ESPCOMP)/bt/host/nimble/nimble/nimble/host/store/ram/src/*.c) \ +# $(wildcard $(ESPCOMP)/bt/host/nimble/nimble/nimble/host/util/src/*.c) \ +# $(wildcard $(ESPCOMP)/bt/host/nimble/nimble/nimble/src/*.c) \ +# $(wildcard $(ESPCOMP)/bt/host/nimble/nimble/porting/nimble/src/*.c) \ +# $(wildcard $(ESPCOMP)/bt/host/nimble/nimble/porting/npl/freertos/src/*.c) \ +# ) +#endif +# +#$(BUILD)/$(ESPCOMP)/esp_eth/src/esp_eth_mac_dm9051.o: CFLAGS += -fno-strict-aliasing +#ESPIDF_ESP_ETH_O = $(patsubst %.c,%.o,$(wildcard $(ESPCOMP)/esp_eth/src/*.c)) +# +#ESPIDF_XTENSA_O = $(patsubst %.c,%.o,\ +# $(wildcard $(ESPCOMP)/xtensa/*.c) \ +# $(wildcard $(ESPCOMP)/xtensa/esp32/*.c) \ +# ) +#else +#ESPIDF_JSON_O = $(patsubst %.c,%.o,$(wildcard $(ESPCOMP)/json/cJSON/cJSON*.c)) +# +#ESPIDF_ETHERNET_O = $(patsubst %.c,%.o,\ +# $(wildcard $(ESPCOMP)/ethernet/*.c) \ +# $(wildcard $(ESPCOMP)/ethernet/eth_phy/*.c) \ +# ) +#endif +# +#OBJ_ESPIDF = +#LIB_ESPIDF = +#BUILD_ESPIDF_LIB = $(BUILD)/esp-idf +# +#define gen_espidf_lib_rule +#OBJ_ESPIDF += $(addprefix $$(BUILD)/,$(2)) +#LIB_ESPIDF += $(1) +#$(BUILD_ESPIDF_LIB)/$(1)/lib$(1).a: $(addprefix $$(BUILD)/,$(2)) +# $(ECHO) "AR $$@" +# $(Q)$(AR) cru $$@ $$^ +#endef +# +#$(eval $(call gen_espidf_lib_rule,bootloader_support,$(ESPIDF_BOOTLOADER_SUPPORT_O))) +#$(eval $(call gen_espidf_lib_rule,driver,$(ESPIDF_DRIVER_O))) +#$(eval $(call gen_espidf_lib_rule,efuse,$(ESPIDF_EFUSE_O))) +#$(eval $(call gen_espidf_lib_rule,esp32,$(ESPIDF_ESP32_O))) +#$(eval $(call gen_espidf_lib_rule,esp_ringbuf,$(ESPIDF_ESP_RINGBUF_O))) +#$(eval $(call gen_espidf_lib_rule,heap,$(ESPIDF_HEAP_O))) +#$(eval $(call gen_espidf_lib_rule,soc,$(ESPIDF_SOC_O))) +#$(eval $(call gen_espidf_lib_rule,cxx,$(ESPIDF_CXX_O))) +#$(eval $(call gen_espidf_lib_rule,pthread,$(ESPIDF_PTHREAD_O))) +#$(eval $(call gen_espidf_lib_rule,freertos,$(ESPIDF_FREERTOS_O))) +#$(eval $(call gen_espidf_lib_rule,vfs,$(ESPIDF_VFS_O))) +#$(eval $(call gen_espidf_lib_rule,json,$(ESPIDF_JSON_O))) +#$(eval $(call gen_espidf_lib_rule,log,$(ESPIDF_LOG_O))) +#$(eval $(call gen_espidf_lib_rule,xtensa-debug-module,$(ESPIDF_XTENSA_DEBUG_MODULE_O))) +#$(eval $(call gen_espidf_lib_rule,tcpip_adapter,$(ESPIDF_TCPIP_ADAPTER_O))) +#$(eval $(call gen_espidf_lib_rule,app_trace,$(ESPIDF_APP_TRACE_O))) +#$(eval $(call gen_espidf_lib_rule,app_update,$(ESPIDF_APP_UPDATE_O))) +#$(eval $(call gen_espidf_lib_rule,newlib,$(ESPIDF_NEWLIB_O))) +#$(eval $(call gen_espidf_lib_rule,nvs_flash,$(ESPIDF_NVS_FLASH_O))) +#$(eval $(call gen_espidf_lib_rule,smartconfig_ack,$(ESPIDF_SMARTCONFIG_ACK_O))) +#$(eval $(call gen_espidf_lib_rule,spi_flash,$(ESPIDF_SPI_FLASH_O))) +#$(eval $(call gen_espidf_lib_rule,ulp,$(ESPIDF_ULP_O))) +#$(eval $(call gen_espidf_lib_rule,lwip,$(ESPIDF_LWIP_O))) +#$(eval $(call gen_espidf_lib_rule,mbedtls,$(ESPIDF_MBEDTLS_O))) +#$(eval $(call gen_espidf_lib_rule,mdns,$(ESPIDF_MDNS_O))) +#$(eval $(call gen_espidf_lib_rule,wpa_supplicant,$(ESPIDF_WPA_SUPPLICANT_O))) +#$(eval $(call gen_espidf_lib_rule,sdmmc,$(ESPIDF_SDMMC_O))) +# +#ifeq ($(ESPIDF_CURHASH),$(ESPIDF_SUPHASH_V4)) +#$(eval $(call gen_espidf_lib_rule,esp_common,$(ESPIDF_ESP_COMMON_O))) +#$(eval $(call gen_espidf_lib_rule,esp_event,$(ESPIDF_ESP_EVENT_O))) +#$(eval $(call gen_espidf_lib_rule,esp_wifi,$(ESPIDF_ESP_WIFI_O))) +#ifeq ($(CONFIG_BT_NIMBLE_ENABLED),y) +#$(eval $(call gen_espidf_lib_rule,bt_nimble,$(ESPIDF_BT_NIMBLE_O))) +#endif +#$(eval $(call gen_espidf_lib_rule,esp_eth,$(ESPIDF_ESP_ETH_O))) +#$(eval $(call gen_espidf_lib_rule,xtensa,$(ESPIDF_XTENSA_O))) +#else +#$(eval $(call gen_espidf_lib_rule,ethernet,$(ESPIDF_ETHERNET_O))) +#endif +# +## Create all destination build dirs before compiling IDF source +#OBJ_ESPIDF_DIRS = $(sort $(dir $(OBJ_ESPIDF))) $(BUILD_ESPIDF_LIB) $(addprefix $(BUILD_ESPIDF_LIB)/,$(LIB_ESPIDF)) +#$(OBJ_ESPIDF): | $(OBJ_ESPIDF_DIRS) +#$(OBJ_ESPIDF_DIRS): +# $(MKDIR) -p $@ +# +## Make all IDF object files depend on sdkconfig +#$(OBJ_ESPIDF): $(SDKCONFIG_H) +# +## Add all IDF components to the set of libraries +#LIB = $(foreach lib,$(LIB_ESPIDF),$(BUILD_ESPIDF_LIB)/$(lib)/lib$(lib).a) +# +################################################################################# +## ESP IDF ldgen +# +#LDGEN_FRAGMENTS = $(shell find $(ESPCOMP) -name "*.lf") +# +#ifeq ($(ESPIDF_CURHASH),$(ESPIDF_SUPHASH_V4)) +# +#LDGEN_LIBRARIES=$(foreach lib,$(LIB_ESPIDF),$(BUILD_ESPIDF_LIB)/$(lib)/lib$(lib).a) +# +#$(BUILD_ESPIDF_LIB)/ldgen_libraries: $(LDGEN_LIBRARIES) $(ESPIDF)/make/ldgen.mk +# printf "$(foreach library,$(LDGEN_LIBRARIES),$(library)\n)" > $(BUILD_ESPIDF_LIB)/ldgen_libraries +# +#$(BUILD)/esp32.project.ld: $(ESPCOMP)/esp32/ld/esp32.project.ld.in $(LDGEN_FRAGMENTS) $(SDKCONFIG_COMBINED) $(BUILD_ESPIDF_LIB)/ldgen_libraries +# $(ECHO) "GEN $@" +# $(Q)$(PYTHON) $(ESPIDF)/tools/ldgen/ldgen.py \ +# --input $< \ +# --output $@ \ +# --config $(SDKCONFIG_COMBINED) \ +# --kconfig $(ESPIDF)/Kconfig \ +# --fragments $(LDGEN_FRAGMENTS) \ +# --libraries-file $(BUILD_ESPIDF_LIB)/ldgen_libraries \ +# --env "IDF_TARGET=esp32" \ +# --env "IDF_CMAKE=n" \ +# --env "COMPONENT_KCONFIGS=$(ESPCOMP_KCONFIGS)" \ +# --env "COMPONENT_KCONFIGS_PROJBUILD=$(ESPCOMP_KCONFIGS_PROJBUILD)" \ +# --env "IDF_PATH=$(ESPIDF)" \ +# --objdump $(OBJDUMP) +# +#else +# +#LDGEN_SECTIONS_INFO = $(foreach lib,$(LIB_ESPIDF),$(BUILD_ESPIDF_LIB)/$(lib)/lib$(lib).a.sections_info) +#LDGEN_SECTION_INFOS = $(BUILD_ESPIDF_LIB)/ldgen.section_infos +# +#define gen_sections_info_rule +#$(1).sections_info: $(1) +# $(ECHO) "GEN $(1).sections_info" +# $(Q)$(OBJDUMP) -h $(1) > $(1).sections_info +#endef +# +#$(eval $(foreach lib,$(LIB_ESPIDF),$(eval $(call gen_sections_info_rule,$(BUILD_ESPIDF_LIB)/$(lib)/lib$(lib).a)))) +# +#$(LDGEN_SECTION_INFOS): $(LDGEN_SECTIONS_INFO) $(ESPIDF)/make/ldgen.mk +# $(Q)printf "$(foreach info,$(LDGEN_SECTIONS_INFO),$(info)\n)" > $@ +# +#$(BUILD)/esp32.project.ld: $(ESPCOMP)/esp32/ld/esp32.project.ld.in $(LDGEN_FRAGMENTS) $(SDKCONFIG_COMBINED) $(LDGEN_SECTION_INFOS) +# $(ECHO) "GEN $@" +# $(Q)$(PYTHON) $(ESPIDF)/tools/ldgen/ldgen.py \ +# --input $< \ +# --output $@ \ +# --config $(SDKCONFIG_COMBINED) \ +# --kconfig $(ESPIDF)/Kconfig \ +# --fragments $(LDGEN_FRAGMENTS) \ +# --sections $(LDGEN_SECTION_INFOS) \ +# --env "IDF_TARGET=esp32" \ +# --env "IDF_CMAKE=n" \ +# --env "COMPONENT_KCONFIGS=$(ESPCOMP_KCONFIGS)" \ +# --env "COMPONENT_KCONFIGS_PROJBUILD=$(ESPCOMP_KCONFIGS_PROJBUILD)" \ +# --env "IDF_PATH=$(ESPIDF)" +# +#endif +# +################################################################################# +## Main targets +# +#all: $(BUILD)/firmware.bin +# +#.PHONY: idf-version deploy erase +# +#idf-version: +# $(ECHO) "ESP IDF supported hash: $(ESPIDF_SUPHASH)" +# +#$(BUILD)/firmware.bin: $(BUILD)/bootloader.bin $(BUILD)/partitions.bin $(BUILD)/application.bin +# $(ECHO) "Create $@" +# $(Q)$(PYTHON) makeimg.py $^ $@ +# +#deploy: $(BUILD)/firmware.bin +# $(ECHO) "Writing $^ to the board" +# $(Q)$(ESPTOOL) --chip esp32 --port $(PORT) --baud $(BAUD) write_flash -z --flash_mode $(FLASH_MODE) --flash_freq $(FLASH_FREQ) 0x1000 $^ +# +#erase: +# $(ECHO) "Erasing flash" +# $(Q)$(ESPTOOL) --chip esp32 --port $(PORT) --baud $(BAUD) erase_flash +# +################################################################################# +## Declarations to build the application +# +#OBJ = $(OBJ_MP) +# +#APP_LD_ARGS = +#APP_LD_ARGS += $(LDFLAGS_MOD) +#APP_LD_ARGS += $(addprefix -T,$(LD_FILES)) +#APP_LD_ARGS += --start-group +#APP_LD_ARGS += -L$(dir $(LIBGCC_FILE_NAME)) -lgcc +#APP_LD_ARGS += -L$(dir $(LIBSTDCXX_FILE_NAME)) -lstdc++ +#APP_LD_ARGS += $(LIBC_LIBM) +#ifeq ($(ESPIDF_CURHASH),$(ESPIDF_SUPHASH_V4)) +#APP_LD_ARGS += -L$(ESPCOMP)/xtensa/esp32 -lhal +#APP_LD_ARGS += -L$(ESPCOMP)/bt/controller/lib -lbtdm_app +#APP_LD_ARGS += -L$(ESPCOMP)/esp_wifi/lib_esp32 -lcore -lmesh -lnet80211 -lphy -lrtc -lpp -lsmartconfig -lcoexist +#else +#APP_LD_ARGS += $(ESPCOMP)/esp32/libhal.a +#APP_LD_ARGS += -L$(ESPCOMP)/esp32/lib -lcore -lmesh -lnet80211 -lphy -lrtc -lpp -lwpa -lsmartconfig -lcoexist -lwps -lwpa2 +#endif +#APP_LD_ARGS += $(OBJ) +#APP_LD_ARGS += $(LIB) +#APP_LD_ARGS += --end-group +# +#$(BUILD)/esp32_out.ld: $(SDKCONFIG_H) +# $(Q)$(CC) -I$(BUILD) -C -P -x c -E $(ESPCOMP)/esp32/ld/esp32.ld -o $@ +# +#$(BUILD)/application.bin: $(BUILD)/application.elf +# $(ECHO) "Create $@" +# $(Q)$(ESPTOOL) --chip esp32 elf2image --flash_mode $(FLASH_MODE) --flash_freq $(FLASH_FREQ) --flash_size $(FLASH_SIZE) $< +# +#$(BUILD)/application.elf: $(OBJ) $(LIB) $(BUILD)/esp32_out.ld $(BUILD)/esp32.project.ld +# $(ECHO) "LINK $@" +# $(Q)$(LD) $(LDFLAGS) -o $@ $(APP_LD_ARGS) +# $(Q)$(SIZE) $@ +# +#define compile_cxx +#$(ECHO) "CXX $<" +#$(Q)$(CXX) $(CXXFLAGS) -c -MD -o $@ $< +#@# The following fixes the dependency file. +#@# See http://make.paulandlesley.org/autodep.html for details. +#@# Regex adjusted from the above to play better with Windows paths, etc. +#@$(CP) $(@:.o=.d) $(@:.o=.P); \ +# $(SED) -e 's/#.*//' -e 's/^.*: *//' -e 's/ *\\$$//' \ +# -e '/^$$/ d' -e 's/$$/ :/' < $(@:.o=.d) >> $(@:.o=.P); \ +# $(RM) -f $(@:.o=.d) +#endef +# +#vpath %.cpp . $(TOP) +#$(BUILD)/%.o: %.cpp +# $(call compile_cxx) +# +################################################################################# +## Declarations to build the bootloader +# +#BOOTLOADER_LIB_DIR = $(BUILD)/bootloader +#BOOTLOADER_LIB_ALL = +# +#ifeq ($(ESPIDF_CURHASH),$(ESPIDF_SUPHASH_V4)) +#$(BUILD)/bootloader/$(ESPCOMP)/%.o: CFLAGS += -DBOOTLOADER_BUILD=1 -I$(ESPCOMP)/bootloader_support/include_priv -I$(ESPCOMP)/bootloader_support/include -I$(ESPCOMP)/efuse/include -I$(ESPCOMP)/esp_rom/include -Wno-error=format \ +# -I$(ESPCOMP)/esp_common/include \ +# -I$(ESPCOMP)/xtensa/include \ +# -I$(ESPCOMP)/xtensa/esp32/include +#else +#$(BUILD)/bootloader/$(ESPCOMP)/%.o: CFLAGS += -DBOOTLOADER_BUILD=1 -I$(ESPCOMP)/bootloader_support/include_priv -I$(ESPCOMP)/bootloader_support/include -I$(ESPCOMP)/micro-ecc/micro-ecc -I$(ESPCOMP)/efuse/include -I$(ESPCOMP)/esp32 -Wno-error=format +#endif +# +## libbootloader_support.a +#BOOTLOADER_LIB_ALL += bootloader_support +#BOOTLOADER_LIB_BOOTLOADER_SUPPORT_OBJ = $(addprefix $(BUILD)/bootloader/$(ESPCOMP)/,\ +# bootloader_support/src/bootloader_clock.o \ +# bootloader_support/src/bootloader_common.o \ +# bootloader_support/src/bootloader_flash.o \ +# bootloader_support/src/bootloader_init.o \ +# bootloader_support/src/bootloader_random.o \ +# bootloader_support/src/bootloader_utility.o \ +# bootloader_support/src/flash_qio_mode.o \ +# bootloader_support/src/esp_image_format.o \ +# bootloader_support/src/flash_encrypt.o \ +# bootloader_support/src/flash_partitions.o \ +# ) +# +#ifeq ($(ESPIDF_CURHASH),$(ESPIDF_SUPHASH_V4)) +#BOOTLOADER_LIB_BOOTLOADER_SUPPORT_OBJ += $(addprefix $(BUILD)/bootloader/$(ESPCOMP)/,\ +# bootloader_support/src/esp32/bootloader_sha.o \ +# bootloader_support/src/bootloader_flash_config.o \ +# bootloader_support/src/esp32/secure_boot.o \ +# ) +#else +#BOOTLOADER_LIB_BOOTLOADER_SUPPORT_OBJ += $(addprefix $(BUILD)/bootloader/$(ESPCOMP)/,\ +# bootloader_support/src/bootloader_sha.o \ +# bootloader_support/src/secure_boot_signatures.o \ +# bootloader_support/src/secure_boot.o \ +# ) +#endif +# +#$(BOOTLOADER_LIB_DIR)/libbootloader_support.a: $(BOOTLOADER_LIB_BOOTLOADER_SUPPORT_OBJ) +# $(ECHO) "AR $@" +# $(Q)$(AR) cr $@ $^ +# +## liblog.a +#BOOTLOADER_LIB_ALL += log +#BOOTLOADER_LIB_LOG_OBJ = $(addprefix $(BUILD)/bootloader/$(ESPCOMP)/,\ +# log/log.o \ +# ) +#$(BOOTLOADER_LIB_DIR)/liblog.a: $(BOOTLOADER_LIB_LOG_OBJ) +# $(ECHO) "AR $@" +# $(Q)$(AR) cr $@ $^ +# +## libspi_flash.a +#BOOTLOADER_LIB_ALL += spi_flash +#BOOTLOADER_LIB_SPI_FLASH_OBJ = $(addprefix $(BUILD)/bootloader/$(ESPCOMP)/,\ +# spi_flash/spi_flash_rom_patch.o \ +# ) +#$(BOOTLOADER_LIB_DIR)/libspi_flash.a: $(BOOTLOADER_LIB_SPI_FLASH_OBJ) +# $(ECHO) "AR $@" +# $(Q)$(AR) cr $@ $^ +# +#ifeq ($(ESPIDF_CURHASH),$(ESPIDF_SUPHASH_V3)) +## libmicro-ecc.a +#BOOTLOADER_LIB_ALL += micro-ecc +#BOOTLOADER_LIB_MICRO_ECC_OBJ = $(addprefix $(BUILD)/bootloader/$(ESPCOMP)/,\ +# micro-ecc/micro-ecc/uECC.o \ +# ) +#$(BOOTLOADER_LIB_DIR)/libmicro-ecc.a: $(BOOTLOADER_LIB_MICRO_ECC_OBJ) +# $(ECHO) "AR $@" +# $(Q)$(AR) cr $@ $^ +#endif +# +## libsoc.a +#$(BUILD)/bootloader/$(ESPCOMP)/soc/esp32/rtc_clk.o: CFLAGS += -fno-jump-tables -fno-tree-switch-conversion +#BOOTLOADER_LIB_ALL += soc +#BOOTLOADER_LIB_SOC_OBJ = $(addprefix $(BUILD)/bootloader/$(ESPCOMP)/soc/,\ +# esp32/cpu_util.o \ +# esp32/gpio_periph.o \ +# esp32/rtc_clk.o \ +# esp32/rtc_clk_init.o \ +# esp32/rtc_init.o \ +# esp32/rtc_periph.o \ +# esp32/rtc_pm.o \ +# esp32/rtc_sleep.o \ +# esp32/rtc_time.o \ +# esp32/rtc_wdt.o \ +# esp32/sdio_slave_periph.o \ +# esp32/sdmmc_periph.o \ +# esp32/soc_memory_layout.o \ +# esp32/spi_periph.o \ +# src/memory_layout_utils.o \ +# ) +#$(BOOTLOADER_LIB_DIR)/libsoc.a: $(BOOTLOADER_LIB_SOC_OBJ) +# $(ECHO) "AR $@" +# $(Q)$(AR) cr $@ $^ +# +## libmain.a +#BOOTLOADER_LIB_ALL += main +#BOOTLOADER_LIB_MAIN_OBJ = $(addprefix $(BUILD)/bootloader/$(ESPCOMP)/,\ +# bootloader/subproject/main/bootloader_start.o \ +# ) +#$(BOOTLOADER_LIB_DIR)/libmain.a: $(BOOTLOADER_LIB_MAIN_OBJ) +# $(ECHO) "AR $@" +# $(Q)$(AR) cr $@ $^ +# +## all objects files +#BOOTLOADER_OBJ_ALL = \ +# $(BOOTLOADER_LIB_BOOTLOADER_SUPPORT_OBJ) \ +# $(BOOTLOADER_LIB_LOG_OBJ) \ +# $(BOOTLOADER_LIB_SPI_FLASH_OBJ) \ +# $(BOOTLOADER_LIB_MICRO_ECC_OBJ) \ +# $(BOOTLOADER_LIB_SOC_OBJ) \ +# $(BOOTLOADER_LIB_MAIN_OBJ) +# +#$(BOOTLOADER_OBJ_ALL): $(SDKCONFIG_H) +# +#BOOTLOADER_LIBS = +#BOOTLOADER_LIBS += -Wl,--start-group +#BOOTLOADER_LIBS += $(BOOTLOADER_OBJ) +#BOOTLOADER_LIBS += -L$(BUILD)/bootloader $(addprefix -l,$(BOOTLOADER_LIB_ALL)) +#ifeq ($(ESPIDF_CURHASH),$(ESPIDF_SUPHASH_V4)) +#BOOTLOADER_LIBS += -L$(ESPCOMP)/esp_wifi/lib_esp32 -lrtc +#else +#BOOTLOADER_LIBS += -L$(ESPCOMP)/esp32/lib -lrtc +#endif +#BOOTLOADER_LIBS += -L$(dir $(LIBGCC_FILE_NAME)) -lgcc +#BOOTLOADER_LIBS += -Wl,--end-group +# +#BOOTLOADER_LDFLAGS = +#BOOTLOADER_LDFLAGS += -nostdlib +#BOOTLOADER_LDFLAGS += -L$(ESPIDF)/lib +#BOOTLOADER_LDFLAGS += -L$(ESPIDF)/ld +#BOOTLOADER_LDFLAGS += -u call_user_start_cpu0 +#BOOTLOADER_LDFLAGS += -Wl,--gc-sections +#BOOTLOADER_LDFLAGS += -static +#BOOTLOADER_LDFLAGS += -Wl,-EL +#BOOTLOADER_LDFLAGS += -Wl,-Map=$(@:.elf=.map) -Wl,--cref +#BOOTLOADER_LDFLAGS += -T $(ESPCOMP)/bootloader/subproject/main/esp32.bootloader.ld +#BOOTLOADER_LDFLAGS += -T $(ESPCOMP)/bootloader/subproject/main/esp32.bootloader.rom.ld +#ifeq ($(ESPIDF_CURHASH),$(ESPIDF_SUPHASH_V4)) +#BOOTLOADER_LDFLAGS += -T $(ESPCOMP)/esp_rom/esp32/ld/esp32.rom.ld +#BOOTLOADER_LDFLAGS += -T $(ESPCOMP)/esp_rom/esp32/ld/esp32.rom.newlib-funcs.ld +#else +#BOOTLOADER_LDFLAGS += -T $(ESPCOMP)/esp32/ld/esp32.rom.ld +#BOOTLOADER_LDFLAGS += -T $(ESPCOMP)/esp32/ld/esp32.rom.spiram_incompatible_fns.ld +#endif +#BOOTLOADER_LDFLAGS += -T $(ESPCOMP)/esp32/ld/esp32.peripherals.ld +# +#BOOTLOADER_OBJ_DIRS = $(sort $(dir $(BOOTLOADER_OBJ_ALL))) +#$(BOOTLOADER_OBJ_ALL): | $(BOOTLOADER_OBJ_DIRS) +#$(BOOTLOADER_OBJ_DIRS): +# $(MKDIR) -p $@ +# +#$(BUILD)/bootloader/%.o: %.c +# $(call compile_c) +# +#$(BUILD)/bootloader.bin: $(BUILD)/bootloader.elf +# $(ECHO) "Create $@" +# $(Q)$(ESPTOOL) --chip esp32 elf2image --flash_mode $(FLASH_MODE) --flash_freq $(FLASH_FREQ) --flash_size $(FLASH_SIZE) $< +# +#$(BUILD)/bootloader.elf: $(BOOTLOADER_OBJ) $(addprefix $(BOOTLOADER_LIB_DIR)/lib,$(addsuffix .a,$(BOOTLOADER_LIB_ALL))) +# $(ECHO) "LINK $@" +# $(Q)$(CC) $(BOOTLOADER_LDFLAGS) -o $@ $(BOOTLOADER_LIBS) +# +################################################################################# +## Declarations to build the partitions +# +#PYTHON2 ?= python2 +# +## Can be overriden by mkconfigboard.mk. +#PART_SRC ?= partitions.csv +# +#$(BUILD)/partitions.bin: $(PART_SRC) +# $(ECHO) "Create $@" +# $(Q)$(PYTHON2) $(ESPCOMP)/partition_table/gen_esp32part.py -q $< $@ +# +################################################################################# +# +#include $(TOP)/py/mkrules.mk diff --git a/ports/esp32/Makefile b/ports/esp32/Makefile index fe4787b7ae338..ef826ea64e438 100644 --- a/ports/esp32/Makefile +++ b/ports/esp32/Makefile @@ -761,20 +761,30 @@ APP_LD_ARGS += -L$(ESPCOMP)/esp32/lib -lcore -lmesh -lnet80211 -lphy -lrtc -lpp endif APP_LD_ARGS += $(OBJ) APP_LD_ARGS += $(LIB) +APP_LD_ARGS += $(BUILD)/plat_relo.o APP_LD_ARGS += --end-group $(BUILD)/esp32_out.ld: $(SDKCONFIG_H) $(Q)$(CC) -I$(BUILD) -C -P -x c -E $(ESPCOMP)/esp32/ld/esp32.ld -o $@ -$(BUILD)/application.bin: $(BUILD)/application.elf +$(BUILD)/application.bin: $(BUILD)/application.elf $(BUILD)/application.sym $(ECHO) "Create $@" $(Q)$(ESPTOOL) --chip esp32 elf2image --flash_mode $(FLASH_MODE) --flash_freq $(FLASH_FREQ) --flash_size $(FLASH_SIZE) $< -$(BUILD)/application.elf: $(OBJ) $(LIB) $(BUILD)/esp32_out.ld $(BUILD)/esp32.project.ld +# Memory map produced by nm, for troubleshooting purposes, not required for operation +$(BUILD)/application.sym: $(BUILD)/application.elf + $(ECHO) "Create $@" + $(Q)xtensa-esp32-elf-nm -l -n $< >$@ + +$(BUILD)/application.elf: $(OBJ) $(LIB) $(BUILD)/esp32_out.ld $(BUILD)/esp32.project.ld $(BUILD)/plat_relo.o $(ECHO) "LINK $@" $(Q)$(LD) $(LDFLAGS) -o $@ $(APP_LD_ARGS) $(Q)$(SIZE) $@ +$(BUILD)/plat_relo.s: plat_relo.py + $(ECHO) "GEN platform relocations" + $(Q)./plat_relo.py --tab >$@ + define compile_cxx $(ECHO) "CXX $<" $(Q)$(CXX) $(CXXFLAGS) -c -MD -o $@ $< diff --git a/ports/esp32/plat_relo.py b/ports/esp32/plat_relo.py new file mode 100755 index 0000000000000..a2444813c3479 --- /dev/null +++ b/ports/esp32/plat_relo.py @@ -0,0 +1,101 @@ +#! /usr/bin/python3 +# +# Linking table for platform-specific functions. +# +# The entry points below list a number of functions in the MP firmware that are to be exposed to +# native modules. This is typically used to expose ESP-IDF functions so they can be called directly +# from C code in dynamically loaded native modules. The native module can then #include the esp-idf +# header files as usual and call the functions normally. One major limitation is that only +# functions with 6 arguments or fewer can be called due to the way the linking is done. +# +# The way the linking is done is as follows: +# - in the firmware a `plat_relo_tab` table is created with the addresses of the target functions. +# This table works just like the `mp_fun_table` and could be merged with it, but is kept separate +# here in order not to deal with #ifdefs and #includes to include platform-specific functions. +# - in the native module a `plat_relo_stubs` section is created which contains an 8-byte code stub +# for each target function. Each code stub loads the index into the plat_relo_tab for the function +# and jumps to a common stub. +# - a common stub then retrieves the address of the plat_relo_tab from the mp_fun_table, indexes +# into the plat_relo_tab to retrieve the function's address, and calls the function. +# - the result of all this is that a call from a native module to an esp-idf function goes through +# an intermediate function call, which is the stub code. The stub code passes the 6 argument +# registers through, but does not handle any additional stack-based arguments, hence the +# limitation to 6 arguments. +# - given the way the xtensa calling convention works it does not appear possible to eliminate the +# intermediate function, i.e. write stubs that only do jumps such that the target function can +# return directly to the native module call site, the reason is that after a call instruction +# there is no register available to perform the mp_fun_table and plat_relo_tab traversal. +# - the code below generates the plat_relo_tab for inclusion in the MP firmware (--tab option), +# generates the assembler code for inclusion in native modules (no cmdline option), and can +# optionally also generate equivalent, but larger, C stub code for inclusion in native modules +# (-c option). +# - note that everything here is untyped, i.e., there is no regard for the C type of the target +# function and it is assumed that arguments and return value can be passed through the stub +# function by copying some registers. + +import sys + +entry_points = [ + "pcnt_unit_config", + "pcnt_get_counter_value", + "pcnt_counter_pause", + "pcnt_counter_resume", + "pcnt_counter_clear", + "pcnt_intr_disable", + "mp_micropython_mem_info", + ] + +if len(sys.argv) > 1 and sys.argv[1] == '--tab': + # Platform relocation table compiled into the MP firmware + print('\t.file\t"plat_relo.py"') + print('\t.section\t.rodata.plat_relo_tab,"a",@progbits') + print('\t.align\t4') + print('\t.global\tplat_relo_tab') + print('\t.type\tplat_relo_tab, @object') + print('plat_relo_tab:') + for ep in entry_points: + print('\t.word\t' + ep) + print('\t.size\tplat_relo_tab, .-plat_relo_tab') + +elif len(sys.argv) > 1 and sys.argv[1] == '-c': + # Proof-of-concept C stubs compiled into the native module + print('#include "py/nativeglue.h"') + print('typedef int six_arg_fun(int a1, int a2, int a3, int a4, int a5, int a6);') + print('') + for i, ep in enumerate(entry_points): + print('int {}(int a1, int a2, int a3, int a4, int a5, int a6) {{'.format(ep)) + print(' return ((six_arg_fun**)mp_fun_table.plat_relo_tab) [{}](a1, a2, a3, a4, a5, a6);'.format(i)) + print('}'); + +else: + # Assembly stubs compiled into the native module + print('\t.file\t"plat_relo.py"') + print('\t.section\t.text.plat_relo_stubs,"ax",@progbits') + print('\t.global\tplat_relo_tab') + + print('plat_relo_stubs:') + for i, ep in enumerate(entry_points): + print('\t.global\t' + ep) + #print('\t.type\t' + ep + ',@function') + print('\t.align\t4') + print(ep + ':') + print('\tentry\tsp,32') # enter subroutine, 3-byte instruction + print('\tmovi.n\ta15,{}'.format(i)) # move immediate, 2-byte instr, plat_relo_tab index + print('\tj\t.L1') # jump, 3-byte instr + + print('\t.literal_position') + print('\t.literal\t.LC0,mp_fun_table') + print('\t.align\t4') + print('.L1:') + for i in range(6): + dest = 11-i # destination register, start with reg 11 which is the 5th arg + print('\tmov\ta{},a{}'.format(dest, dest-4)) # moves a7->a11, a6->a10, ..., a2->a6 + print('\tl32r\ta3,.LC0') # load mp_fun_table base address + print('\tslli\ta2,a15,2') # multiply index by 4 + print('\tl32i\ta3,a3,320') # load plat_relo_tab address + print('\tadd.n\ta2,a3,a2') # index into plat_relo_tab + print('\tl32i\ta2,a2,0') # load target function address + print('\tcallx4\ta2') + print('\tmov\ta2,a6') # move return value from a6 to a2 + print('\tretw.n') + print('\t.size\tplat_relo_stubs, .-plat_relo_stubs') diff --git a/py/dynruntime.mk b/py/dynruntime.mk index 8b65745afdf78..9b459cf6a7a64 100644 --- a/py/dynruntime.mk +++ b/py/dynruntime.mk @@ -128,6 +128,11 @@ $(BUILD)/%.o: %.c $(CONFIG_H) Makefile $(ECHO) "CC $<" $(Q)$(CROSS)gcc $(CFLAGS) -o $@ -c $< +# Build .o from .s source files +%.o: %.s $(CONFIG_H) Makefile + $(ECHO) "AS $<" + $(Q)$(CROSS)gcc $(CFLAGS) -o $@ -c $< + # Build .mpy from .py source files $(BUILD)/%.mpy: %.py $(ECHO) "MPY $<" diff --git a/py/nativeglue.c b/py/nativeglue.c index 2e0ac56ca574b..068f01e7abbbb 100644 --- a/py/nativeglue.c +++ b/py/nativeglue.c @@ -253,6 +253,8 @@ STATIC double mp_obj_get_float_to_d(mp_obj_t o) { #endif +extern int plat_relo_tab[]; + // these must correspond to the respective enum in nativeglue.h const mp_fun_table_t mp_fun_table = { mp_const_none, @@ -344,6 +346,7 @@ const mp_fun_table_t mp_fun_table = { &mp_stream_readinto_obj, &mp_stream_unbuffered_readline_obj, &mp_stream_write_obj, + (void*)&plat_relo_tab, }; #endif // MICROPY_EMIT_NATIVE diff --git a/py/nativeglue.h b/py/nativeglue.h index 1b6d9cc7a069a..4f0a9acf47e13 100644 --- a/py/nativeglue.h +++ b/py/nativeglue.h @@ -170,6 +170,7 @@ typedef struct _mp_fun_table_t { const mp_obj_fun_builtin_var_t *stream_readinto_obj; const mp_obj_fun_builtin_var_t *stream_unbuffered_readline_obj; const mp_obj_fun_builtin_var_t *stream_write_obj; + const int *plat_relo_tab; } mp_fun_table_t; extern const mp_fun_table_t mp_fun_table; diff --git a/tools/mpy_ld.py b/tools/mpy_ld.py index 31c3912991c48..ddb8316549eab 100755 --- a/tools/mpy_ld.py +++ b/tools/mpy_ld.py @@ -678,6 +678,7 @@ def link_objects(env, native_qstr_vals_len, native_qstr_objs_len): 'mp_stream_readinto_obj', 'mp_stream_unbuffered_readline_obj', 'mp_stream_write_obj', + 'plat_relo_tab', ]) } for sym in env.unresolved_syms: From d08a5c5f347c59518198f9ccf3834658c5eab477 Mon Sep 17 00:00:00 2001 From: Thorsten von Eicken Date: Wed, 19 Feb 2020 08:43:11 -0800 Subject: [PATCH 2/6] add comments --- examples/natmod/esp-idf/counter.c | 2 ++ ports/esp32/plat_relo.py | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/examples/natmod/esp-idf/counter.c b/examples/natmod/esp-idf/counter.c index fdc6637b22ac6..8d6a0d9b91b23 100644 --- a/examples/natmod/esp-idf/counter.c +++ b/examples/natmod/esp-idf/counter.c @@ -32,6 +32,8 @@ STATIC mp_obj_t counter_init(mp_obj_t x_obj) { // Define a Python reference to the function above STATIC MP_DEFINE_CONST_FUN_OBJ_1(counter_init_obj, counter_init); +// Another function that calls into the firmware, this time to mp_micropython_mem_info() +// just as a simple demo which prints something to make it easy to see whether it's working or not. STATIC mp_obj_t mem_info() { extern int mp_micropython_mem_info(int, int); mp_printf(&mp_plat_print, "mp_fun_table is at 0x%x\n", &mp_fun_table); diff --git a/ports/esp32/plat_relo.py b/ports/esp32/plat_relo.py index a2444813c3479..c74f53362ba75 100755 --- a/ports/esp32/plat_relo.py +++ b/ports/esp32/plat_relo.py @@ -1,5 +1,5 @@ #! /usr/bin/python3 -# + # Linking table for platform-specific functions. # # The entry points below list a number of functions in the MP firmware that are to be exposed to From a683d28bb2cb2ad735947331e76687a6181192d7 Mon Sep 17 00:00:00 2001 From: Thorsten von Eicken Date: Wed, 19 Feb 2020 22:41:14 -0800 Subject: [PATCH 3/6] rework to perform direct relocation of call sites --- examples/natmod/esp-idf/Makefile | 11 +- examples/natmod/esp-idf/counter.c | 1 - examples/natmod/esp-idf/dyn_esp32.mk | 709 +-------------------------- ports/esp32/plat_relo.py | 93 +--- py/persistentcode.c | 10 + tools/mpy_ld.py | 70 ++- 6 files changed, 85 insertions(+), 809 deletions(-) diff --git a/examples/natmod/esp-idf/Makefile b/examples/natmod/esp-idf/Makefile index 2874ce5be95f2..0aa05d76cca5e 100644 --- a/examples/natmod/esp-idf/Makefile +++ b/examples/natmod/esp-idf/Makefile @@ -20,13 +20,4 @@ ARCH = xtensawin # Include to get the rules for compiling and linking the module include dyn_esp32.mk include $(MPY_DIR)/py/dynruntime.mk - -#plat_stubs.c: $(MPY_DIR)/ports/esp32/plat_relo.py -# $(ECHO) "GEN $@" -# $(Q)mkdir -p build -# $(Q)$< -c >$@ - -build/plat_stubs.s: $(MPY_DIR)/ports/esp32/plat_relo.py - $(ECHO) "GEN $@" - $(Q)mkdir -p build - $(Q)$< >$@ +CFLAGS += -std=gnu99 # override -std=c99 in dynruntime.mk diff --git a/examples/natmod/esp-idf/counter.c b/examples/natmod/esp-idf/counter.c index 8d6a0d9b91b23..fd8c5247a926c 100644 --- a/examples/natmod/esp-idf/counter.c +++ b/examples/natmod/esp-idf/counter.c @@ -37,7 +37,6 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(counter_init_obj, counter_init); STATIC mp_obj_t mem_info() { extern int mp_micropython_mem_info(int, int); mp_printf(&mp_plat_print, "mp_fun_table is at 0x%x\n", &mp_fun_table); - mp_printf(&mp_plat_print, "plat_relo_tab is at 0x%x\n", mp_fun_table.plat_relo_tab); mp_printf(&mp_plat_print, "calling mp_micropython_mem_info\n"); int i = mp_micropython_mem_info(0, 0); mp_printf(&mp_plat_print, "mp_micropython_mem_info: %d/0x%x\n", i, i); diff --git a/examples/natmod/esp-idf/dyn_esp32.mk b/examples/natmod/esp-idf/dyn_esp32.mk index b03fd2407f258..5995e80090513 100644 --- a/examples/natmod/esp-idf/dyn_esp32.mk +++ b/examples/natmod/esp-idf/dyn_esp32.mk @@ -57,11 +57,11 @@ SDKCONFIG := $(addprefix $(PORT_DIR)/,$(SDKCONFIG)) # the git hash of the currently supported ESP IDF version ESPIDF_SUPHASH_V3 := 6ccb4cf5b7d1fdddb8c2492f9cbc926abaf230df -ESPIDF_SUPHASH_V4 := 310beae373446ceb9a4ad9b36b5428d7fdf2705f +ESPIDF_SUPHASH_V4 := 463a9d8b7f9af8205222b80707f9bdbba7c530e1 define print_supported_git_hash $(info Supported git hash (v3.3): $(ESPIDF_SUPHASH_V3)) -$(info Supported git hash (v4.0-beta1) (experimental): $(ESPIDF_SUPHASH_V4)) +$(info Supported git hash (v4.0) (experimental): $(ESPIDF_SUPHASH_V4)) endef # paths to ESP IDF and its components @@ -237,708 +237,3 @@ endif endif CFLAGS = $(INC_ESPCOMP) $(CFLAGS_MOD) - -SRC_O += build/plat_stubs.o - -## these flags are common to C and C++ compilation -#CFLAGS_COMMON = -Os -ffunction-sections -fdata-sections -fstrict-volatile-bitfields \ -# -mlongcalls -nostdlib \ -# -Wall -Werror -Wno-error=unused-function -Wno-error=unused-but-set-variable \ -# -Wno-error=unused-variable -Wno-error=deprecated-declarations \ -# -DESP_PLATFORM -# -#CFLAGS_BASE = -std=gnu99 $(CFLAGS_COMMON) -DMBEDTLS_CONFIG_FILE='"mbedtls/esp_config.h"' -DHAVE_CONFIG_H -#CFLAGS = $(CFLAGS_BASE) $(INC) $(INC_ESPCOMP) -#CFLAGS += -DIDF_VER=\"$(IDF_VER)\" -#CFLAGS += $(CFLAGS_MOD) $(CFLAGS_EXTRA) -#CFLAGS += -I$(BOARD_DIR) -# -#ifeq ($(ESPIDF_CURHASH),$(ESPIDF_SUPHASH_V4)) -#CFLAGS += -DMICROPY_ESP_IDF_4=1 -#endif -# -## this is what ESPIDF uses for c++ compilation -#CXXFLAGS = -std=gnu++11 $(CFLAGS_COMMON) $(INC) $(INC_ESPCOMP) -# -#LDFLAGS = -nostdlib -Map=$(@:.elf=.map) --cref -#LDFLAGS += --gc-sections -static -EL -#LDFLAGS += -u call_user_start_cpu0 -u uxTopUsedPriority -u ld_include_panic_highint_hdl -#LDFLAGS += -u __cxa_guard_dummy # so that implementation of static guards is taken from cxx_guards.o instead of libstdc++.a -#LDFLAGS += -L$(ESPCOMP)/esp32/ld -#LDFLAGS += -L$(ESPCOMP)/esp_rom/esp32/ld -#LDFLAGS += -T $(BUILD)/esp32_out.ld -#LDFLAGS += -T $(BUILD)/esp32.project.ld -#LDFLAGS += -T esp32.rom.ld -#LDFLAGS += -T esp32.rom.libgcc.ld -#LDFLAGS += -T esp32.peripherals.ld -# -#LIBGCC_FILE_NAME = $(shell $(CC) $(CFLAGS) -print-libgcc-file-name) -#LIBSTDCXX_FILE_NAME = $(shell $(CXX) $(CXXFLAGS) -print-file-name=libstdc++.a) -# -## Debugging/Optimization -#ifeq ($(DEBUG), 1) -#CFLAGS += -g -#COPT = -O0 -#else -##CFLAGS += -fdata-sections -ffunction-sections -#COPT += -Os -DNDEBUG -##LDFLAGS += --gc-sections -#endif -# -## Options for mpy-cross -#MPY_CROSS_FLAGS += -march=xtensawin -# -## Enable SPIRAM support if CONFIG_ESP32_SPIRAM_SUPPORT=y in sdkconfig -#ifeq ($(CONFIG_ESP32_SPIRAM_SUPPORT),y) -#CFLAGS_COMMON += -mfix-esp32-psram-cache-issue -#LIBC_LIBM = $(ESPCOMP)/newlib/lib/libc-psram-workaround.a $(ESPCOMP)/newlib/lib/libm-psram-workaround.a -#else -## Additional newlib symbols that can only be used with spiram disabled. -#ifeq ($(ESPIDF_CURHASH),$(ESPIDF_SUPHASH_V4)) -#LDFLAGS += -T esp32.rom.newlib-funcs.ld -#LDFLAGS += -T esp32.rom.newlib-locale.ld -#LDFLAGS += -T esp32.rom.newlib-data.ld -#else -#LDFLAGS += -T esp32.rom.spiram_incompatible_fns.ld -#endif -#LIBC_LIBM = $(ESPCOMP)/newlib/lib/libc.a $(ESPCOMP)/newlib/lib/libm.a -#endif -# -################################################################################# -## List of MicroPython source and object files -# -#SRC_C = \ -# main.c \ -# uart.c \ -# gccollect.c \ -# mphalport.c \ -# fatfs_port.c \ -# help.c \ -# modutime.c \ -# moduos.c \ -# machine_timer.c \ -# machine_pin.c \ -# machine_touchpad.c \ -# machine_adc.c \ -# machine_dac.c \ -# machine_i2c.c \ -# machine_pwm.c \ -# machine_counter.c \ -# machine_uart.c \ -# modmachine.c \ -# modnetwork.c \ -# network_lan.c \ -# network_ppp.c \ -# nimble.c \ -# modsocket.c \ -# modesp.c \ -# esp32_partition.c \ -# esp32_rmt.c \ -# esp32_ulp.c \ -# modesp32.c \ -# espneopixel.c \ -# machine_hw_spi.c \ -# machine_wdt.c \ -# mpthreadport.c \ -# machine_rtc.c \ -# machine_sdcard.c \ -# $(wildcard $(BOARD_DIR)/*.c) \ -# $(SRC_MOD) -# -#EXTMOD_SRC_C = $(addprefix extmod/,\ -# modonewire.c \ -# modbluetooth_nimble.c \ -# ) -# -#LIB_SRC_C = $(addprefix lib/,\ -# mp-readline/readline.c \ -# netutils/netutils.c \ -# timeutils/timeutils.c \ -# utils/pyexec.c \ -# utils/interrupt_char.c \ -# utils/sys_stdio_mphal.c \ -# ) -# -#DRIVERS_SRC_C = $(addprefix drivers/,\ -# bus/softspi.c \ -# dht/dht.c \ -# ) -# -#OBJ_MP = -#OBJ_MP += $(PY_O) -#OBJ_MP += $(addprefix $(BUILD)/, $(SRC_C:.c=.o)) -#OBJ_MP += $(addprefix $(BUILD)/, $(EXTMOD_SRC_C:.c=.o)) -#OBJ_MP += $(addprefix $(BUILD)/, $(LIB_SRC_C:.c=.o)) -#OBJ_MP += $(addprefix $(BUILD)/, $(DRIVERS_SRC_C:.c=.o)) -# -## List of sources for qstr extraction -#SRC_QSTR += $(SRC_C) $(EXTMOD_SRC_C) $(LIB_SRC_C) $(DRIVERS_SRC_C) -## Append any auto-generated sources that are needed by sources listed in SRC_QSTR -#SRC_QSTR_AUTO_DEPS += -# -################################################################################# -## Generate sdkconfig.h from sdkconfig -# -#$(SDKCONFIG_COMBINED): $(SDKCONFIG) -# $(Q)$(MKDIR) -p $(dir $@) -# $(Q)$(CAT) $^ > $@ -# -#$(SDKCONFIG_H): $(SDKCONFIG_COMBINED) -# $(ECHO) "GEN $@" -# $(Q)$(MKDIR) -p $(dir $@) -# $(Q)$(PYTHON) $(ESPIDF)/tools/kconfig_new/confgen.py \ -# --output header $@ \ -# --config $< \ -# --kconfig $(ESPIDF)/Kconfig \ -# --env "IDF_TARGET=esp32" \ -# --env "IDF_CMAKE=n" \ -# --env "COMPONENT_KCONFIGS=$(ESPCOMP_KCONFIGS)" \ -# --env "COMPONENT_KCONFIGS_PROJBUILD=$(ESPCOMP_KCONFIGS_PROJBUILD)" \ -# --env "IDF_PATH=$(ESPIDF)" -# $(Q)touch $@ -# -#$(HEADER_BUILD)/qstrdefs.generated.h: $(SDKCONFIG_H) $(BOARD_DIR)/mpconfigboard.h -# -################################################################################# -## List of object files from the ESP32 IDF components -# -#ESPIDF_BOOTLOADER_SUPPORT_O = $(patsubst %.c,%.o,\ -# $(filter-out $(ESPCOMP)/bootloader_support/src/bootloader_init.c,\ -# $(wildcard $(ESPCOMP)/bootloader_support/src/*.c) \ -# $(wildcard $(ESPCOMP)/bootloader_support/src/idf/*.c) \ -# )) -# -#ESPIDF_DRIVER_O = $(patsubst %.c,%.o,$(wildcard $(ESPCOMP)/driver/*.c)) -# -#ESPIDF_EFUSE_O = $(patsubst %.c,%.o,\ -# $(wildcard $(ESPCOMP)/efuse/esp32/*.c)\ -# $(wildcard $(ESPCOMP)/efuse/src/*.c)\ -# ) -# -#$(BUILD)/$(ESPCOMP)/esp32/dport_access.o: CFLAGS += -Wno-array-bounds -#ESPIDF_ESP32_O = \ -# $(patsubst %.c,%.o,$(wildcard $(ESPCOMP)/esp32/*.c)) \ -# $(patsubst %.c,%.o,$(wildcard $(ESPCOMP)/esp32/hwcrypto/*.c)) \ -# $(patsubst %.S,%.o,$(wildcard $(ESPCOMP)/esp32/*.S)) \ -# -#ESPIDF_ESP_RINGBUF_O = $(patsubst %.c,%.o,$(wildcard $(ESPCOMP)/esp_ringbuf/*.c)) -# -#ESPIDF_HEAP_O = $(patsubst %.c,%.o,$(wildcard $(ESPCOMP)/heap/*.c)) -# -#ESPIDF_SOC_O = $(patsubst %.c,%.o,\ -# $(wildcard $(ESPCOMP)/soc/esp32/*.c) \ -# $(wildcard $(ESPCOMP)/soc/src/*.c) \ -# $(wildcard $(ESPCOMP)/soc/src/hal/*.c) \ -# ) -# -#ESPIDF_CXX_O = $(patsubst %.cpp,%.o,$(wildcard $(ESPCOMP)/cxx/*.cpp)) -# -#ESPIDF_PTHREAD_O = $(patsubst %.c,%.o,$(wildcard $(ESPCOMP)/pthread/*.c)) -# -## Assembler .S files need only basic flags, and in particular should not have -## -Os because that generates subtly different code. -## We also need custom CFLAGS for .c files because FreeRTOS has headers with -## generic names (eg queue.h) which can clash with other files in the port. -#ifeq ($(ESPIDF_CURHASH),$(ESPIDF_SUPHASH_V4)) -#CFLAGS_ASM = -I$(BUILD) -I$(ESPCOMP)/esp32/include -I$(ESPCOMP)/soc/esp32/include -I$(ESPCOMP)/freertos/include/freertos -I. -I$(ESPCOMP)/xtensa/include -I$(ESPCOMP)/xtensa/esp32/include -I$(ESPCOMP)/esp_common/include -#else -#CFLAGS_ASM = -I$(BUILD) -I$(ESPCOMP)/esp32/include -I$(ESPCOMP)/soc/esp32/include -I$(ESPCOMP)/freertos/include/freertos -I. -#endif -#$(BUILD)/$(ESPCOMP)/freertos/portasm.o: CFLAGS = $(CFLAGS_ASM) -#$(BUILD)/$(ESPCOMP)/freertos/xtensa_context.o: CFLAGS = $(CFLAGS_ASM) -#$(BUILD)/$(ESPCOMP)/freertos/xtensa_intr_asm.o: CFLAGS = $(CFLAGS_ASM) -#$(BUILD)/$(ESPCOMP)/freertos/xtensa_vectors.o: CFLAGS = $(CFLAGS_ASM) -#$(BUILD)/$(ESPCOMP)/freertos/xtensa_vector_defaults.o: CFLAGS = $(CFLAGS_ASM) -#$(BUILD)/$(ESPCOMP)/freertos/%.o: CFLAGS = $(CFLAGS_BASE) -I. -I$(BUILD) $(INC_ESPCOMP) -I$(ESPCOMP)/freertos/include/freertos -D_ESP_FREERTOS_INTERNAL -#ESPIDF_FREERTOS_O = \ -# $(patsubst %.c,%.o,$(wildcard $(ESPCOMP)/freertos/*.c)) \ -# $(patsubst %.S,%.o,$(wildcard $(ESPCOMP)/freertos/*.S)) \ -# -#ESPIDF_VFS_O = $(patsubst %.c,%.o,$(wildcard $(ESPCOMP)/vfs/*.c)) -# -#ESPIDF_LOG_O = $(patsubst %.c,%.o,$(wildcard $(ESPCOMP)/log/*.c)) -# -#ESPIDF_XTENSA_DEBUG_MODULE_O = $(patsubst %.c,%.o,$(wildcard $(ESPCOMP)/xtensa-debug-module/*.c)) -# -#ESPIDF_TCPIP_ADAPTER_O = $(patsubst %.c,%.o,$(wildcard $(ESPCOMP)/tcpip_adapter/*.c)) -# -#ESPIDF_APP_TRACE_O = $(patsubst %.c,%.o,$(wildcard $(ESPCOMP)/app_trace/*.c)) -# -#ESPIDF_APP_UPDATE_O = $(patsubst %.c,%.o,$(wildcard $(ESPCOMP)/app_update/*.c)) -# -#ESPIDF_NEWLIB_O = $(patsubst %.c,%.o,$(wildcard $(ESPCOMP)/newlib/*.c)) -# -#ESPIDF_NVS_FLASH_O = $(patsubst %.cpp,%.o,$(wildcard $(ESPCOMP)/nvs_flash/src/*.cpp)) -# -#ESPIDF_SMARTCONFIG_ACK_O = $(patsubst %.c,%.o,$(wildcard $(ESPCOMP)/smartconfig_ack/*.c)) -# -#ESPIDF_SPI_FLASH_O = $(patsubst %.c,%.o,$(wildcard $(ESPCOMP)/spi_flash/*.c)) -# -#ESPIDF_ULP_O = $(patsubst %.c,%.o,$(wildcard $(ESPCOMP)/ulp/*.c)) -# -#$(BUILD)/$(ESPCOMP)/lwip/%.o: CFLAGS += -Wno-address -Wno-unused-variable -Wno-unused-but-set-variable -#ESPIDF_LWIP_O = $(patsubst %.c,%.o,\ -# $(wildcard $(ESPCOMP)/lwip/apps/dhcpserver/*.c) \ -# $(wildcard $(ESPCOMP)/lwip/lwip/src/api/*.c) \ -# $(wildcard $(ESPCOMP)/lwip/lwip/src/apps/sntp/*.c) \ -# $(wildcard $(ESPCOMP)/lwip/lwip/src/core/*.c) \ -# $(wildcard $(ESPCOMP)/lwip/lwip/src/core/*/*.c) \ -# $(wildcard $(ESPCOMP)/lwip/lwip/src/netif/*.c) \ -# $(wildcard $(ESPCOMP)/lwip/lwip/src/netif/*/*.c) \ -# $(wildcard $(ESPCOMP)/lwip/lwip/src/netif/*/*/*.c) \ -# $(wildcard $(ESPCOMP)/lwip/port/esp32/*.c) \ -# $(wildcard $(ESPCOMP)/lwip/port/esp32/*/*.c) \ -# ) -# -#ESPIDF_MBEDTLS_O = $(patsubst %.c,%.o,\ -# $(wildcard $(ESPCOMP)/mbedtls/mbedtls/library/*.c) \ -# $(wildcard $(ESPCOMP)/mbedtls/port/*.c) \ -# $(wildcard $(ESPCOMP)/mbedtls/port/esp32/*.c) \ -# ) -# -#ESPIDF_MDNS_O = $(patsubst %.c,%.o,$(wildcard $(ESPCOMP)/mdns/*.c)) -# -#ifeq ($(ESPIDF_CURHASH),$(ESPIDF_SUPHASH_V4)) -#$(BUILD)/$(ESPCOMP)/wpa_supplicant/%.o: CFLAGS += -DESP_SUPPLICANT -DIEEE8021X_EAPOL -DEAP_PEER_METHOD -DEAP_TLS -DEAP_TTLS -DEAP_PEAP -DEAP_MSCHAPv2 -DUSE_WPA2_TASK -DCONFIG_WPS2 -DCONFIG_WPS_PIN -DUSE_WPS_TASK -DESPRESSIF_USE -DESP32_WORKAROUND -DCONFIG_ECC -D__ets__ -Wno-strict-aliasing -I$(ESPCOMP)/wpa_supplicant/src -Wno-implicit-function-declaration -#else -#$(BUILD)/$(ESPCOMP)/wpa_supplicant/%.o: CFLAGS += -DEMBEDDED_SUPP -DIEEE8021X_EAPOL -DEAP_PEER_METHOD -DEAP_MSCHAPv2 -DEAP_TTLS -DEAP_TLS -DEAP_PEAP -DUSE_WPA2_TASK -DCONFIG_WPS2 -DCONFIG_WPS_PIN -DUSE_WPS_TASK -DESPRESSIF_USE -DESP32_WORKAROUND -DALLOW_EVEN_MOD -D__ets__ -Wno-strict-aliasing -#endif -#ESPIDF_WPA_SUPPLICANT_O = $(patsubst %.c,%.o,\ -# $(wildcard $(ESPCOMP)/wpa_supplicant/port/*.c) \ -# $(wildcard $(ESPCOMP)/wpa_supplicant/src/*/*.c) \ -# ) -# -#ESPIDF_SDMMC_O = $(patsubst %.c,%.o,$(wildcard $(ESPCOMP)/sdmmc/*.c)) -# -#ifeq ($(ESPIDF_CURHASH),$(ESPIDF_SUPHASH_V4)) -#ESPIDF_ESP_COMMON_O = $(patsubst %.c,%.o,$(wildcard $(ESPCOMP)/esp_common/src/*.c)) -# -#ESPIDF_ESP_EVENT_O = $(patsubst %.c,%.o,$(wildcard $(ESPCOMP)/esp_event/*.c)) -# -#ESPIDF_ESP_WIFI_O = $(patsubst %.c,%.o,$(wildcard $(ESPCOMP)/esp_wifi/src/*.c)) -# -#ifeq ($(CONFIG_BT_NIMBLE_ENABLED),y) -#ESPIDF_BT_NIMBLE_O = $(patsubst %.c,%.o,\ -# $(wildcard $(ESPCOMP)/bt/controller/*.c) \ -# $(wildcard $(ESPCOMP)/bt/common/btc/core/*.c) \ -# $(wildcard $(ESPCOMP)/bt/common/osi/*.c) \ -# $(wildcard $(ESPCOMP)/bt/host/nimble/esp-hci/src/*.c) \ -# $(wildcard $(ESPCOMP)/bt/host/nimble/nimble/ext/tinycrypt/src/*.c) \ -# $(wildcard $(ESPCOMP)/bt/host/nimble/nimble/nimble/host/services/ans/src/*.c) \ -# $(wildcard $(ESPCOMP)/bt/host/nimble/nimble/nimble/host/services/bas/src/*.c) \ -# $(wildcard $(ESPCOMP)/bt/host/nimble/nimble/nimble/host/services/gap/src/*.c) \ -# $(wildcard $(ESPCOMP)/bt/host/nimble/nimble/nimble/host/services/gatt/src/*.c) \ -# $(wildcard $(ESPCOMP)/bt/host/nimble/nimble/nimble/host/services/ias/src/*.c) \ -# $(wildcard $(ESPCOMP)/bt/host/nimble/nimble/nimble/host/services/lls/src/*.c) \ -# $(wildcard $(ESPCOMP)/bt/host/nimble/nimble/nimble/host/services/tps/src/*.c) \ -# $(wildcard $(ESPCOMP)/bt/host/nimble/nimble/nimble/host/src/*.c) \ -# $(wildcard $(ESPCOMP)/bt/host/nimble/nimble/nimble/host/store/config/src/ble_store_config.c) \ -# $(wildcard $(ESPCOMP)/bt/host/nimble/nimble/nimble/host/store/config/src/ble_store_nvs.c) \ -# $(wildcard $(ESPCOMP)/bt/host/nimble/nimble/nimble/host/store/ram/src/*.c) \ -# $(wildcard $(ESPCOMP)/bt/host/nimble/nimble/nimble/host/util/src/*.c) \ -# $(wildcard $(ESPCOMP)/bt/host/nimble/nimble/nimble/src/*.c) \ -# $(wildcard $(ESPCOMP)/bt/host/nimble/nimble/porting/nimble/src/*.c) \ -# $(wildcard $(ESPCOMP)/bt/host/nimble/nimble/porting/npl/freertos/src/*.c) \ -# ) -#endif -# -#$(BUILD)/$(ESPCOMP)/esp_eth/src/esp_eth_mac_dm9051.o: CFLAGS += -fno-strict-aliasing -#ESPIDF_ESP_ETH_O = $(patsubst %.c,%.o,$(wildcard $(ESPCOMP)/esp_eth/src/*.c)) -# -#ESPIDF_XTENSA_O = $(patsubst %.c,%.o,\ -# $(wildcard $(ESPCOMP)/xtensa/*.c) \ -# $(wildcard $(ESPCOMP)/xtensa/esp32/*.c) \ -# ) -#else -#ESPIDF_JSON_O = $(patsubst %.c,%.o,$(wildcard $(ESPCOMP)/json/cJSON/cJSON*.c)) -# -#ESPIDF_ETHERNET_O = $(patsubst %.c,%.o,\ -# $(wildcard $(ESPCOMP)/ethernet/*.c) \ -# $(wildcard $(ESPCOMP)/ethernet/eth_phy/*.c) \ -# ) -#endif -# -#OBJ_ESPIDF = -#LIB_ESPIDF = -#BUILD_ESPIDF_LIB = $(BUILD)/esp-idf -# -#define gen_espidf_lib_rule -#OBJ_ESPIDF += $(addprefix $$(BUILD)/,$(2)) -#LIB_ESPIDF += $(1) -#$(BUILD_ESPIDF_LIB)/$(1)/lib$(1).a: $(addprefix $$(BUILD)/,$(2)) -# $(ECHO) "AR $$@" -# $(Q)$(AR) cru $$@ $$^ -#endef -# -#$(eval $(call gen_espidf_lib_rule,bootloader_support,$(ESPIDF_BOOTLOADER_SUPPORT_O))) -#$(eval $(call gen_espidf_lib_rule,driver,$(ESPIDF_DRIVER_O))) -#$(eval $(call gen_espidf_lib_rule,efuse,$(ESPIDF_EFUSE_O))) -#$(eval $(call gen_espidf_lib_rule,esp32,$(ESPIDF_ESP32_O))) -#$(eval $(call gen_espidf_lib_rule,esp_ringbuf,$(ESPIDF_ESP_RINGBUF_O))) -#$(eval $(call gen_espidf_lib_rule,heap,$(ESPIDF_HEAP_O))) -#$(eval $(call gen_espidf_lib_rule,soc,$(ESPIDF_SOC_O))) -#$(eval $(call gen_espidf_lib_rule,cxx,$(ESPIDF_CXX_O))) -#$(eval $(call gen_espidf_lib_rule,pthread,$(ESPIDF_PTHREAD_O))) -#$(eval $(call gen_espidf_lib_rule,freertos,$(ESPIDF_FREERTOS_O))) -#$(eval $(call gen_espidf_lib_rule,vfs,$(ESPIDF_VFS_O))) -#$(eval $(call gen_espidf_lib_rule,json,$(ESPIDF_JSON_O))) -#$(eval $(call gen_espidf_lib_rule,log,$(ESPIDF_LOG_O))) -#$(eval $(call gen_espidf_lib_rule,xtensa-debug-module,$(ESPIDF_XTENSA_DEBUG_MODULE_O))) -#$(eval $(call gen_espidf_lib_rule,tcpip_adapter,$(ESPIDF_TCPIP_ADAPTER_O))) -#$(eval $(call gen_espidf_lib_rule,app_trace,$(ESPIDF_APP_TRACE_O))) -#$(eval $(call gen_espidf_lib_rule,app_update,$(ESPIDF_APP_UPDATE_O))) -#$(eval $(call gen_espidf_lib_rule,newlib,$(ESPIDF_NEWLIB_O))) -#$(eval $(call gen_espidf_lib_rule,nvs_flash,$(ESPIDF_NVS_FLASH_O))) -#$(eval $(call gen_espidf_lib_rule,smartconfig_ack,$(ESPIDF_SMARTCONFIG_ACK_O))) -#$(eval $(call gen_espidf_lib_rule,spi_flash,$(ESPIDF_SPI_FLASH_O))) -#$(eval $(call gen_espidf_lib_rule,ulp,$(ESPIDF_ULP_O))) -#$(eval $(call gen_espidf_lib_rule,lwip,$(ESPIDF_LWIP_O))) -#$(eval $(call gen_espidf_lib_rule,mbedtls,$(ESPIDF_MBEDTLS_O))) -#$(eval $(call gen_espidf_lib_rule,mdns,$(ESPIDF_MDNS_O))) -#$(eval $(call gen_espidf_lib_rule,wpa_supplicant,$(ESPIDF_WPA_SUPPLICANT_O))) -#$(eval $(call gen_espidf_lib_rule,sdmmc,$(ESPIDF_SDMMC_O))) -# -#ifeq ($(ESPIDF_CURHASH),$(ESPIDF_SUPHASH_V4)) -#$(eval $(call gen_espidf_lib_rule,esp_common,$(ESPIDF_ESP_COMMON_O))) -#$(eval $(call gen_espidf_lib_rule,esp_event,$(ESPIDF_ESP_EVENT_O))) -#$(eval $(call gen_espidf_lib_rule,esp_wifi,$(ESPIDF_ESP_WIFI_O))) -#ifeq ($(CONFIG_BT_NIMBLE_ENABLED),y) -#$(eval $(call gen_espidf_lib_rule,bt_nimble,$(ESPIDF_BT_NIMBLE_O))) -#endif -#$(eval $(call gen_espidf_lib_rule,esp_eth,$(ESPIDF_ESP_ETH_O))) -#$(eval $(call gen_espidf_lib_rule,xtensa,$(ESPIDF_XTENSA_O))) -#else -#$(eval $(call gen_espidf_lib_rule,ethernet,$(ESPIDF_ETHERNET_O))) -#endif -# -## Create all destination build dirs before compiling IDF source -#OBJ_ESPIDF_DIRS = $(sort $(dir $(OBJ_ESPIDF))) $(BUILD_ESPIDF_LIB) $(addprefix $(BUILD_ESPIDF_LIB)/,$(LIB_ESPIDF)) -#$(OBJ_ESPIDF): | $(OBJ_ESPIDF_DIRS) -#$(OBJ_ESPIDF_DIRS): -# $(MKDIR) -p $@ -# -## Make all IDF object files depend on sdkconfig -#$(OBJ_ESPIDF): $(SDKCONFIG_H) -# -## Add all IDF components to the set of libraries -#LIB = $(foreach lib,$(LIB_ESPIDF),$(BUILD_ESPIDF_LIB)/$(lib)/lib$(lib).a) -# -################################################################################# -## ESP IDF ldgen -# -#LDGEN_FRAGMENTS = $(shell find $(ESPCOMP) -name "*.lf") -# -#ifeq ($(ESPIDF_CURHASH),$(ESPIDF_SUPHASH_V4)) -# -#LDGEN_LIBRARIES=$(foreach lib,$(LIB_ESPIDF),$(BUILD_ESPIDF_LIB)/$(lib)/lib$(lib).a) -# -#$(BUILD_ESPIDF_LIB)/ldgen_libraries: $(LDGEN_LIBRARIES) $(ESPIDF)/make/ldgen.mk -# printf "$(foreach library,$(LDGEN_LIBRARIES),$(library)\n)" > $(BUILD_ESPIDF_LIB)/ldgen_libraries -# -#$(BUILD)/esp32.project.ld: $(ESPCOMP)/esp32/ld/esp32.project.ld.in $(LDGEN_FRAGMENTS) $(SDKCONFIG_COMBINED) $(BUILD_ESPIDF_LIB)/ldgen_libraries -# $(ECHO) "GEN $@" -# $(Q)$(PYTHON) $(ESPIDF)/tools/ldgen/ldgen.py \ -# --input $< \ -# --output $@ \ -# --config $(SDKCONFIG_COMBINED) \ -# --kconfig $(ESPIDF)/Kconfig \ -# --fragments $(LDGEN_FRAGMENTS) \ -# --libraries-file $(BUILD_ESPIDF_LIB)/ldgen_libraries \ -# --env "IDF_TARGET=esp32" \ -# --env "IDF_CMAKE=n" \ -# --env "COMPONENT_KCONFIGS=$(ESPCOMP_KCONFIGS)" \ -# --env "COMPONENT_KCONFIGS_PROJBUILD=$(ESPCOMP_KCONFIGS_PROJBUILD)" \ -# --env "IDF_PATH=$(ESPIDF)" \ -# --objdump $(OBJDUMP) -# -#else -# -#LDGEN_SECTIONS_INFO = $(foreach lib,$(LIB_ESPIDF),$(BUILD_ESPIDF_LIB)/$(lib)/lib$(lib).a.sections_info) -#LDGEN_SECTION_INFOS = $(BUILD_ESPIDF_LIB)/ldgen.section_infos -# -#define gen_sections_info_rule -#$(1).sections_info: $(1) -# $(ECHO) "GEN $(1).sections_info" -# $(Q)$(OBJDUMP) -h $(1) > $(1).sections_info -#endef -# -#$(eval $(foreach lib,$(LIB_ESPIDF),$(eval $(call gen_sections_info_rule,$(BUILD_ESPIDF_LIB)/$(lib)/lib$(lib).a)))) -# -#$(LDGEN_SECTION_INFOS): $(LDGEN_SECTIONS_INFO) $(ESPIDF)/make/ldgen.mk -# $(Q)printf "$(foreach info,$(LDGEN_SECTIONS_INFO),$(info)\n)" > $@ -# -#$(BUILD)/esp32.project.ld: $(ESPCOMP)/esp32/ld/esp32.project.ld.in $(LDGEN_FRAGMENTS) $(SDKCONFIG_COMBINED) $(LDGEN_SECTION_INFOS) -# $(ECHO) "GEN $@" -# $(Q)$(PYTHON) $(ESPIDF)/tools/ldgen/ldgen.py \ -# --input $< \ -# --output $@ \ -# --config $(SDKCONFIG_COMBINED) \ -# --kconfig $(ESPIDF)/Kconfig \ -# --fragments $(LDGEN_FRAGMENTS) \ -# --sections $(LDGEN_SECTION_INFOS) \ -# --env "IDF_TARGET=esp32" \ -# --env "IDF_CMAKE=n" \ -# --env "COMPONENT_KCONFIGS=$(ESPCOMP_KCONFIGS)" \ -# --env "COMPONENT_KCONFIGS_PROJBUILD=$(ESPCOMP_KCONFIGS_PROJBUILD)" \ -# --env "IDF_PATH=$(ESPIDF)" -# -#endif -# -################################################################################# -## Main targets -# -#all: $(BUILD)/firmware.bin -# -#.PHONY: idf-version deploy erase -# -#idf-version: -# $(ECHO) "ESP IDF supported hash: $(ESPIDF_SUPHASH)" -# -#$(BUILD)/firmware.bin: $(BUILD)/bootloader.bin $(BUILD)/partitions.bin $(BUILD)/application.bin -# $(ECHO) "Create $@" -# $(Q)$(PYTHON) makeimg.py $^ $@ -# -#deploy: $(BUILD)/firmware.bin -# $(ECHO) "Writing $^ to the board" -# $(Q)$(ESPTOOL) --chip esp32 --port $(PORT) --baud $(BAUD) write_flash -z --flash_mode $(FLASH_MODE) --flash_freq $(FLASH_FREQ) 0x1000 $^ -# -#erase: -# $(ECHO) "Erasing flash" -# $(Q)$(ESPTOOL) --chip esp32 --port $(PORT) --baud $(BAUD) erase_flash -# -################################################################################# -## Declarations to build the application -# -#OBJ = $(OBJ_MP) -# -#APP_LD_ARGS = -#APP_LD_ARGS += $(LDFLAGS_MOD) -#APP_LD_ARGS += $(addprefix -T,$(LD_FILES)) -#APP_LD_ARGS += --start-group -#APP_LD_ARGS += -L$(dir $(LIBGCC_FILE_NAME)) -lgcc -#APP_LD_ARGS += -L$(dir $(LIBSTDCXX_FILE_NAME)) -lstdc++ -#APP_LD_ARGS += $(LIBC_LIBM) -#ifeq ($(ESPIDF_CURHASH),$(ESPIDF_SUPHASH_V4)) -#APP_LD_ARGS += -L$(ESPCOMP)/xtensa/esp32 -lhal -#APP_LD_ARGS += -L$(ESPCOMP)/bt/controller/lib -lbtdm_app -#APP_LD_ARGS += -L$(ESPCOMP)/esp_wifi/lib_esp32 -lcore -lmesh -lnet80211 -lphy -lrtc -lpp -lsmartconfig -lcoexist -#else -#APP_LD_ARGS += $(ESPCOMP)/esp32/libhal.a -#APP_LD_ARGS += -L$(ESPCOMP)/esp32/lib -lcore -lmesh -lnet80211 -lphy -lrtc -lpp -lwpa -lsmartconfig -lcoexist -lwps -lwpa2 -#endif -#APP_LD_ARGS += $(OBJ) -#APP_LD_ARGS += $(LIB) -#APP_LD_ARGS += --end-group -# -#$(BUILD)/esp32_out.ld: $(SDKCONFIG_H) -# $(Q)$(CC) -I$(BUILD) -C -P -x c -E $(ESPCOMP)/esp32/ld/esp32.ld -o $@ -# -#$(BUILD)/application.bin: $(BUILD)/application.elf -# $(ECHO) "Create $@" -# $(Q)$(ESPTOOL) --chip esp32 elf2image --flash_mode $(FLASH_MODE) --flash_freq $(FLASH_FREQ) --flash_size $(FLASH_SIZE) $< -# -#$(BUILD)/application.elf: $(OBJ) $(LIB) $(BUILD)/esp32_out.ld $(BUILD)/esp32.project.ld -# $(ECHO) "LINK $@" -# $(Q)$(LD) $(LDFLAGS) -o $@ $(APP_LD_ARGS) -# $(Q)$(SIZE) $@ -# -#define compile_cxx -#$(ECHO) "CXX $<" -#$(Q)$(CXX) $(CXXFLAGS) -c -MD -o $@ $< -#@# The following fixes the dependency file. -#@# See http://make.paulandlesley.org/autodep.html for details. -#@# Regex adjusted from the above to play better with Windows paths, etc. -#@$(CP) $(@:.o=.d) $(@:.o=.P); \ -# $(SED) -e 's/#.*//' -e 's/^.*: *//' -e 's/ *\\$$//' \ -# -e '/^$$/ d' -e 's/$$/ :/' < $(@:.o=.d) >> $(@:.o=.P); \ -# $(RM) -f $(@:.o=.d) -#endef -# -#vpath %.cpp . $(TOP) -#$(BUILD)/%.o: %.cpp -# $(call compile_cxx) -# -################################################################################# -## Declarations to build the bootloader -# -#BOOTLOADER_LIB_DIR = $(BUILD)/bootloader -#BOOTLOADER_LIB_ALL = -# -#ifeq ($(ESPIDF_CURHASH),$(ESPIDF_SUPHASH_V4)) -#$(BUILD)/bootloader/$(ESPCOMP)/%.o: CFLAGS += -DBOOTLOADER_BUILD=1 -I$(ESPCOMP)/bootloader_support/include_priv -I$(ESPCOMP)/bootloader_support/include -I$(ESPCOMP)/efuse/include -I$(ESPCOMP)/esp_rom/include -Wno-error=format \ -# -I$(ESPCOMP)/esp_common/include \ -# -I$(ESPCOMP)/xtensa/include \ -# -I$(ESPCOMP)/xtensa/esp32/include -#else -#$(BUILD)/bootloader/$(ESPCOMP)/%.o: CFLAGS += -DBOOTLOADER_BUILD=1 -I$(ESPCOMP)/bootloader_support/include_priv -I$(ESPCOMP)/bootloader_support/include -I$(ESPCOMP)/micro-ecc/micro-ecc -I$(ESPCOMP)/efuse/include -I$(ESPCOMP)/esp32 -Wno-error=format -#endif -# -## libbootloader_support.a -#BOOTLOADER_LIB_ALL += bootloader_support -#BOOTLOADER_LIB_BOOTLOADER_SUPPORT_OBJ = $(addprefix $(BUILD)/bootloader/$(ESPCOMP)/,\ -# bootloader_support/src/bootloader_clock.o \ -# bootloader_support/src/bootloader_common.o \ -# bootloader_support/src/bootloader_flash.o \ -# bootloader_support/src/bootloader_init.o \ -# bootloader_support/src/bootloader_random.o \ -# bootloader_support/src/bootloader_utility.o \ -# bootloader_support/src/flash_qio_mode.o \ -# bootloader_support/src/esp_image_format.o \ -# bootloader_support/src/flash_encrypt.o \ -# bootloader_support/src/flash_partitions.o \ -# ) -# -#ifeq ($(ESPIDF_CURHASH),$(ESPIDF_SUPHASH_V4)) -#BOOTLOADER_LIB_BOOTLOADER_SUPPORT_OBJ += $(addprefix $(BUILD)/bootloader/$(ESPCOMP)/,\ -# bootloader_support/src/esp32/bootloader_sha.o \ -# bootloader_support/src/bootloader_flash_config.o \ -# bootloader_support/src/esp32/secure_boot.o \ -# ) -#else -#BOOTLOADER_LIB_BOOTLOADER_SUPPORT_OBJ += $(addprefix $(BUILD)/bootloader/$(ESPCOMP)/,\ -# bootloader_support/src/bootloader_sha.o \ -# bootloader_support/src/secure_boot_signatures.o \ -# bootloader_support/src/secure_boot.o \ -# ) -#endif -# -#$(BOOTLOADER_LIB_DIR)/libbootloader_support.a: $(BOOTLOADER_LIB_BOOTLOADER_SUPPORT_OBJ) -# $(ECHO) "AR $@" -# $(Q)$(AR) cr $@ $^ -# -## liblog.a -#BOOTLOADER_LIB_ALL += log -#BOOTLOADER_LIB_LOG_OBJ = $(addprefix $(BUILD)/bootloader/$(ESPCOMP)/,\ -# log/log.o \ -# ) -#$(BOOTLOADER_LIB_DIR)/liblog.a: $(BOOTLOADER_LIB_LOG_OBJ) -# $(ECHO) "AR $@" -# $(Q)$(AR) cr $@ $^ -# -## libspi_flash.a -#BOOTLOADER_LIB_ALL += spi_flash -#BOOTLOADER_LIB_SPI_FLASH_OBJ = $(addprefix $(BUILD)/bootloader/$(ESPCOMP)/,\ -# spi_flash/spi_flash_rom_patch.o \ -# ) -#$(BOOTLOADER_LIB_DIR)/libspi_flash.a: $(BOOTLOADER_LIB_SPI_FLASH_OBJ) -# $(ECHO) "AR $@" -# $(Q)$(AR) cr $@ $^ -# -#ifeq ($(ESPIDF_CURHASH),$(ESPIDF_SUPHASH_V3)) -## libmicro-ecc.a -#BOOTLOADER_LIB_ALL += micro-ecc -#BOOTLOADER_LIB_MICRO_ECC_OBJ = $(addprefix $(BUILD)/bootloader/$(ESPCOMP)/,\ -# micro-ecc/micro-ecc/uECC.o \ -# ) -#$(BOOTLOADER_LIB_DIR)/libmicro-ecc.a: $(BOOTLOADER_LIB_MICRO_ECC_OBJ) -# $(ECHO) "AR $@" -# $(Q)$(AR) cr $@ $^ -#endif -# -## libsoc.a -#$(BUILD)/bootloader/$(ESPCOMP)/soc/esp32/rtc_clk.o: CFLAGS += -fno-jump-tables -fno-tree-switch-conversion -#BOOTLOADER_LIB_ALL += soc -#BOOTLOADER_LIB_SOC_OBJ = $(addprefix $(BUILD)/bootloader/$(ESPCOMP)/soc/,\ -# esp32/cpu_util.o \ -# esp32/gpio_periph.o \ -# esp32/rtc_clk.o \ -# esp32/rtc_clk_init.o \ -# esp32/rtc_init.o \ -# esp32/rtc_periph.o \ -# esp32/rtc_pm.o \ -# esp32/rtc_sleep.o \ -# esp32/rtc_time.o \ -# esp32/rtc_wdt.o \ -# esp32/sdio_slave_periph.o \ -# esp32/sdmmc_periph.o \ -# esp32/soc_memory_layout.o \ -# esp32/spi_periph.o \ -# src/memory_layout_utils.o \ -# ) -#$(BOOTLOADER_LIB_DIR)/libsoc.a: $(BOOTLOADER_LIB_SOC_OBJ) -# $(ECHO) "AR $@" -# $(Q)$(AR) cr $@ $^ -# -## libmain.a -#BOOTLOADER_LIB_ALL += main -#BOOTLOADER_LIB_MAIN_OBJ = $(addprefix $(BUILD)/bootloader/$(ESPCOMP)/,\ -# bootloader/subproject/main/bootloader_start.o \ -# ) -#$(BOOTLOADER_LIB_DIR)/libmain.a: $(BOOTLOADER_LIB_MAIN_OBJ) -# $(ECHO) "AR $@" -# $(Q)$(AR) cr $@ $^ -# -## all objects files -#BOOTLOADER_OBJ_ALL = \ -# $(BOOTLOADER_LIB_BOOTLOADER_SUPPORT_OBJ) \ -# $(BOOTLOADER_LIB_LOG_OBJ) \ -# $(BOOTLOADER_LIB_SPI_FLASH_OBJ) \ -# $(BOOTLOADER_LIB_MICRO_ECC_OBJ) \ -# $(BOOTLOADER_LIB_SOC_OBJ) \ -# $(BOOTLOADER_LIB_MAIN_OBJ) -# -#$(BOOTLOADER_OBJ_ALL): $(SDKCONFIG_H) -# -#BOOTLOADER_LIBS = -#BOOTLOADER_LIBS += -Wl,--start-group -#BOOTLOADER_LIBS += $(BOOTLOADER_OBJ) -#BOOTLOADER_LIBS += -L$(BUILD)/bootloader $(addprefix -l,$(BOOTLOADER_LIB_ALL)) -#ifeq ($(ESPIDF_CURHASH),$(ESPIDF_SUPHASH_V4)) -#BOOTLOADER_LIBS += -L$(ESPCOMP)/esp_wifi/lib_esp32 -lrtc -#else -#BOOTLOADER_LIBS += -L$(ESPCOMP)/esp32/lib -lrtc -#endif -#BOOTLOADER_LIBS += -L$(dir $(LIBGCC_FILE_NAME)) -lgcc -#BOOTLOADER_LIBS += -Wl,--end-group -# -#BOOTLOADER_LDFLAGS = -#BOOTLOADER_LDFLAGS += -nostdlib -#BOOTLOADER_LDFLAGS += -L$(ESPIDF)/lib -#BOOTLOADER_LDFLAGS += -L$(ESPIDF)/ld -#BOOTLOADER_LDFLAGS += -u call_user_start_cpu0 -#BOOTLOADER_LDFLAGS += -Wl,--gc-sections -#BOOTLOADER_LDFLAGS += -static -#BOOTLOADER_LDFLAGS += -Wl,-EL -#BOOTLOADER_LDFLAGS += -Wl,-Map=$(@:.elf=.map) -Wl,--cref -#BOOTLOADER_LDFLAGS += -T $(ESPCOMP)/bootloader/subproject/main/esp32.bootloader.ld -#BOOTLOADER_LDFLAGS += -T $(ESPCOMP)/bootloader/subproject/main/esp32.bootloader.rom.ld -#ifeq ($(ESPIDF_CURHASH),$(ESPIDF_SUPHASH_V4)) -#BOOTLOADER_LDFLAGS += -T $(ESPCOMP)/esp_rom/esp32/ld/esp32.rom.ld -#BOOTLOADER_LDFLAGS += -T $(ESPCOMP)/esp_rom/esp32/ld/esp32.rom.newlib-funcs.ld -#else -#BOOTLOADER_LDFLAGS += -T $(ESPCOMP)/esp32/ld/esp32.rom.ld -#BOOTLOADER_LDFLAGS += -T $(ESPCOMP)/esp32/ld/esp32.rom.spiram_incompatible_fns.ld -#endif -#BOOTLOADER_LDFLAGS += -T $(ESPCOMP)/esp32/ld/esp32.peripherals.ld -# -#BOOTLOADER_OBJ_DIRS = $(sort $(dir $(BOOTLOADER_OBJ_ALL))) -#$(BOOTLOADER_OBJ_ALL): | $(BOOTLOADER_OBJ_DIRS) -#$(BOOTLOADER_OBJ_DIRS): -# $(MKDIR) -p $@ -# -#$(BUILD)/bootloader/%.o: %.c -# $(call compile_c) -# -#$(BUILD)/bootloader.bin: $(BUILD)/bootloader.elf -# $(ECHO) "Create $@" -# $(Q)$(ESPTOOL) --chip esp32 elf2image --flash_mode $(FLASH_MODE) --flash_freq $(FLASH_FREQ) --flash_size $(FLASH_SIZE) $< -# -#$(BUILD)/bootloader.elf: $(BOOTLOADER_OBJ) $(addprefix $(BOOTLOADER_LIB_DIR)/lib,$(addsuffix .a,$(BOOTLOADER_LIB_ALL))) -# $(ECHO) "LINK $@" -# $(Q)$(CC) $(BOOTLOADER_LDFLAGS) -o $@ $(BOOTLOADER_LIBS) -# -################################################################################# -## Declarations to build the partitions -# -#PYTHON2 ?= python2 -# -## Can be overriden by mkconfigboard.mk. -#PART_SRC ?= partitions.csv -# -#$(BUILD)/partitions.bin: $(PART_SRC) -# $(ECHO) "Create $@" -# $(Q)$(PYTHON2) $(ESPCOMP)/partition_table/gen_esp32part.py -q $< $@ -# -################################################################################# -# -#include $(TOP)/py/mkrules.mk diff --git a/ports/esp32/plat_relo.py b/ports/esp32/plat_relo.py index c74f53362ba75..196a9f03e53c9 100755 --- a/ports/esp32/plat_relo.py +++ b/ports/esp32/plat_relo.py @@ -5,36 +5,11 @@ # The entry points below list a number of functions in the MP firmware that are to be exposed to # native modules. This is typically used to expose ESP-IDF functions so they can be called directly # from C code in dynamically loaded native modules. The native module can then #include the esp-idf -# header files as usual and call the functions normally. One major limitation is that only -# functions with 6 arguments or fewer can be called due to the way the linking is done. -# -# The way the linking is done is as follows: -# - in the firmware a `plat_relo_tab` table is created with the addresses of the target functions. -# This table works just like the `mp_fun_table` and could be merged with it, but is kept separate -# here in order not to deal with #ifdefs and #includes to include platform-specific functions. -# - in the native module a `plat_relo_stubs` section is created which contains an 8-byte code stub -# for each target function. Each code stub loads the index into the plat_relo_tab for the function -# and jumps to a common stub. -# - a common stub then retrieves the address of the plat_relo_tab from the mp_fun_table, indexes -# into the plat_relo_tab to retrieve the function's address, and calls the function. -# - the result of all this is that a call from a native module to an esp-idf function goes through -# an intermediate function call, which is the stub code. The stub code passes the 6 argument -# registers through, but does not handle any additional stack-based arguments, hence the -# limitation to 6 arguments. -# - given the way the xtensa calling convention works it does not appear possible to eliminate the -# intermediate function, i.e. write stubs that only do jumps such that the target function can -# return directly to the native module call site, the reason is that after a call instruction -# there is no register available to perform the mp_fun_table and plat_relo_tab traversal. -# - the code below generates the plat_relo_tab for inclusion in the MP firmware (--tab option), -# generates the assembler code for inclusion in native modules (no cmdline option), and can -# optionally also generate equivalent, but larger, C stub code for inclusion in native modules -# (-c option). -# - note that everything here is untyped, i.e., there is no regard for the C type of the target -# function and it is assumed that arguments and return value can be passed through the stub -# function by copying some registers. +# header files as usual and call the functions normally. import sys +# TODO: this needs to be loaded from a file, not hard-coded here entry_points = [ "pcnt_unit_config", "pcnt_get_counter_value", @@ -45,57 +20,13 @@ "mp_micropython_mem_info", ] -if len(sys.argv) > 1 and sys.argv[1] == '--tab': - # Platform relocation table compiled into the MP firmware - print('\t.file\t"plat_relo.py"') - print('\t.section\t.rodata.plat_relo_tab,"a",@progbits') - print('\t.align\t4') - print('\t.global\tplat_relo_tab') - print('\t.type\tplat_relo_tab, @object') - print('plat_relo_tab:') - for ep in entry_points: - print('\t.word\t' + ep) - print('\t.size\tplat_relo_tab, .-plat_relo_tab') - -elif len(sys.argv) > 1 and sys.argv[1] == '-c': - # Proof-of-concept C stubs compiled into the native module - print('#include "py/nativeglue.h"') - print('typedef int six_arg_fun(int a1, int a2, int a3, int a4, int a5, int a6);') - print('') - for i, ep in enumerate(entry_points): - print('int {}(int a1, int a2, int a3, int a4, int a5, int a6) {{'.format(ep)) - print(' return ((six_arg_fun**)mp_fun_table.plat_relo_tab) [{}](a1, a2, a3, a4, a5, a6);'.format(i)) - print('}'); - -else: - # Assembly stubs compiled into the native module - print('\t.file\t"plat_relo.py"') - print('\t.section\t.text.plat_relo_stubs,"ax",@progbits') - print('\t.global\tplat_relo_tab') - - print('plat_relo_stubs:') - for i, ep in enumerate(entry_points): - print('\t.global\t' + ep) - #print('\t.type\t' + ep + ',@function') - print('\t.align\t4') - print(ep + ':') - print('\tentry\tsp,32') # enter subroutine, 3-byte instruction - print('\tmovi.n\ta15,{}'.format(i)) # move immediate, 2-byte instr, plat_relo_tab index - print('\tj\t.L1') # jump, 3-byte instr - - print('\t.literal_position') - print('\t.literal\t.LC0,mp_fun_table') - print('\t.align\t4') - print('.L1:') - for i in range(6): - dest = 11-i # destination register, start with reg 11 which is the 5th arg - print('\tmov\ta{},a{}'.format(dest, dest-4)) # moves a7->a11, a6->a10, ..., a2->a6 - print('\tl32r\ta3,.LC0') # load mp_fun_table base address - print('\tslli\ta2,a15,2') # multiply index by 4 - print('\tl32i\ta3,a3,320') # load plat_relo_tab address - print('\tadd.n\ta2,a3,a2') # index into plat_relo_tab - print('\tl32i\ta2,a2,0') # load target function address - print('\tcallx4\ta2') - print('\tmov\ta2,a6') # move return value from a6 to a2 - print('\tretw.n') - print('\t.size\tplat_relo_stubs, .-plat_relo_stubs') +# Platform relocation table compiled into the MP firmware +print('\t.file\t"plat_relo.py"') +print('\t.section\t.rodata.plat_relo_tab,"a",@progbits') +print('\t.align\t4') +print('\t.global\tplat_relo_tab') +print('\t.type\tplat_relo_tab, @object') +print('plat_relo_tab:') +for ep in entry_points: + print('\t.word\t' + ep) +print('\t.size\tplat_relo_tab, .-plat_relo_tab') diff --git a/py/persistentcode.c b/py/persistentcode.c index 7039f9f57a3c6..4369df831fbf9 100644 --- a/py/persistentcode.c +++ b/py/persistentcode.c @@ -189,12 +189,16 @@ STATIC void arch_link_qstr(uint8_t *pc, bool is_obj, qstr qst) { #endif } +extern size_t plat_relo_tab[]; + void mp_native_relocate(void *ri_in, uint8_t *text, uintptr_t reloc_text) { // Relocate native code reloc_info_t *ri = ri_in; uint8_t op; uintptr_t *addr_to_adjust = NULL; while ((op = read_byte(ri->reader)) != 0xff) { + mp_printf(&mp_plat_print, "mp_native_relocate %d\n", op); + // if lsb==1 then an offset follows the op byte, else addr_to_adjust auto-increments if (op & 1) { // Point to new location to make adjustments size_t addr = read_uint(ri->reader, NULL); @@ -225,14 +229,20 @@ void mp_native_relocate(void *ri_in, uint8_t *text, uintptr_t reloc_text) { } else if (op == 6) { // Destination is mp_fun_table itself dest = (uintptr_t)&mp_fun_table; + } else if (op == 126) { + size_t index = read_uint(ri->reader, NULL); + dest = ((uintptr_t*)&plat_relo_tab)[index]; + mp_printf(&mp_plat_print, "mp_native_relocate: op=%d, index=%d, dest=0x%08x\n", op, index, dest); } else { // Destination is an entry in mp_fun_table dest = ((uintptr_t*)&mp_fun_table)[op - 7]; + mp_printf(&mp_plat_print, "mp_native_relocate: op=%d, dest=0x%08x\n", op, dest); } while (n--) { *addr_to_adjust++ += dest; } } + mp_printf(&mp_plat_print, "mp_native_relocate DONE\n"); } #endif diff --git a/tools/mpy_ld.py b/tools/mpy_ld.py index ddb8316549eab..adaa26cb0afa7 100755 --- a/tools/mpy_ld.py +++ b/tools/mpy_ld.py @@ -34,6 +34,22 @@ sys.path.append(os.path.dirname(__file__) + '/../py') import makeqstrdata as qstrutil +# Platform-specific relocation table, which contains the addresses of a number of platform specific +# functions compiled into the firmware. These entries are used by the dynamic linker to fix-up +# relocation entries in dynamically loaded mpy modules. +# TODO: this needs to be loaded from a file, not hard-coded here. +plat_relo_entries = { name : idx + for idx, name in enumerate([ + "pcnt_unit_config", + "pcnt_get_counter_value", + "pcnt_counter_pause", + "pcnt_counter_resume", + "pcnt_counter_clear", + "pcnt_intr_disable", + "mp_micropython_mem_info", + ]) +} + # MicroPython constants MPY_VERSION = 5 MP_NATIVE_ARCH_X86 = 1 @@ -256,6 +272,10 @@ def isrodata(self): def isbss(self): return self.sec_name.startswith('.bss') + def __repr__(self): # for debugging purposes + keys = ['name', 'sym', 'offset', 'link_addr', 'sec_name'] + return "GOTEntry(" + repr({ k:getattr(self, k) for k in keys}) + ")" + class LiteralEntry: def __init__(self, value, offset): self.value = value @@ -381,8 +401,11 @@ def populate_got(env): dest = '.data.rel.ro' elif got_entry.sec_name.startswith('.bss'): dest = '.bss' + elif got_entry.sec_name == '.external.plat_relo_tab': + dest = 1000+got_entry.sym.plat_relo_offset + print("GOT entry: {}->{} ({})".format(got_entry.name, dest, repr(got_entry))) else: - assert 0, (got_entry.name, got_entry.sec_name) + assert 0, repr(got_entry) env.mpy_relocs.append(('.text', env.got_section.addr + got_entry.offset, dest)) # Print out the final GOT @@ -678,9 +701,10 @@ def link_objects(env, native_qstr_vals_len, native_qstr_objs_len): 'mp_stream_readinto_obj', 'mp_stream_unbuffered_readline_obj', 'mp_stream_write_obj', - 'plat_relo_tab', + #'plat_relo_tab', ]) } + plat_relo_sec = Section('.external.plat_relo_tab', b'', 0) for sym in env.unresolved_syms: assert sym['st_value'] == 0 if sym.name == '_GLOBAL_OFFSET_TABLE_': @@ -693,12 +717,15 @@ def link_objects(env, native_qstr_vals_len, native_qstr_objs_len): sym.section = env.qstr_obj_section elif sym.name in env.known_syms: sym.resolved = env.known_syms[sym.name] + elif sym.name in fun_table: + sym.section = mp_fun_table_sec + sym.mp_fun_table_offset = fun_table[sym.name] + elif sym.name in plat_relo_entries: + sym.section = plat_relo_sec + sym.plat_relo_offset = plat_relo_entries[sym.name] + print("Unresolved: {} -> plat_relo_sec+{}".format(sym.name, sym.plat_relo_offset)) else: - if sym.name in fun_table: - sym.section = mp_fun_table_sec - sym.mp_fun_table_offset = fun_table[sym.name] - else: - raise LinkError('{}: undefined symbol: {}'.format(sym.filename, sym.name)) + raise LinkError('{}: undefined symbol: {}'.format(sym.filename, sym.name)) # Align sections, assign their addresses, and create full_text env.full_text = bytearray(env.arch.asm_jump(8)) # dummy, to be filled in later @@ -767,12 +794,29 @@ def write_qstr(self, s): self.write_bytes(s) def write_reloc(self, base, offset, dest, n): + """ + Write a relocation entry into the mpy that the dynamic linker will have to resolve + when the mpy is loaded. A relocation entry consists of a kind/op byte, followed by an + optional offset word, followed by an optional count word. + - base+offset is the location where the fixup is to be done, base is '.text' or 'rodata'. + - dest is the target whose address needs to be placed into the fixup: 0=string table, + 1=rodata_const_table, 2=bss_const_table, 3..5: unused, 6=mp_fun_table, + 7..126:entry in mp_fun_table, 1000..66535:entry in plat_relo_tab. + - n is number of consecutive words to fix up. + """ need_offset = not (base == self.prev_base and offset == self.prev_offset + 1) self.prev_offset = offset + n - 1 + index = None if dest <= 2: dest = (dest << 1) | (n > 1) + elif dest >= 1000: + # offset into plat_relo_tab + assert dest < 1000+65536 + index = dest-1000 + dest = 126 + print("write_reloc plat_relo_tab+{}".format(index)) else: - assert 6 <= dest <= 127 + assert 6 <= dest < 126 assert n == 1 dest = dest << 1 | need_offset assert 0 <= dest <= 0xfe, dest @@ -783,6 +827,8 @@ def write_reloc(self, base, offset, dest, n): elif base == '.rodata': base = 1 self.write_uint(offset << 1 | base) + if index is not None: + self.write_uint(index) if n > 1: self.write_uint(n) @@ -866,8 +912,12 @@ def build_mpy(env, entry_offset, fmpy, native_qstr_vals, native_qstr_objs): kind = bss_const_table_idx elif kind == 'mp_fun_table': kind = 6 - else: - kind = 7 + kind + elif isinstance(kind, int): + if kind < 1000: + # element of mp_fun_table + kind = 7 + kind + else: # element of plat_relo_tab + pass assert addr % env.arch.word_size == 0, addr offset = addr // env.arch.word_size if kind == prev_kind and base == prev_base and offset == prev_offset + 1: From c8ad1b1f216a874e7041aea7c92fa2291fcc42fc Mon Sep 17 00:00:00 2001 From: Thorsten von Eicken Date: Wed, 19 Feb 2020 22:50:11 -0800 Subject: [PATCH 4/6] clean-up a bit from rework --- ports/esp32/Makefile | 2 +- ports/esp32/plat_relo.py | 2 -- py/nativeglue.c | 3 --- py/nativeglue.h | 1 - tools/mpy_ld.py | 3 ++- 5 files changed, 3 insertions(+), 8 deletions(-) diff --git a/ports/esp32/Makefile b/ports/esp32/Makefile index ef826ea64e438..48683a9cd27a4 100644 --- a/ports/esp32/Makefile +++ b/ports/esp32/Makefile @@ -783,7 +783,7 @@ $(BUILD)/application.elf: $(OBJ) $(LIB) $(BUILD)/esp32_out.ld $(BUILD)/esp32.pro $(BUILD)/plat_relo.s: plat_relo.py $(ECHO) "GEN platform relocations" - $(Q)./plat_relo.py --tab >$@ + $(Q)./plat_relo.py >$@ define compile_cxx $(ECHO) "CXX $<" diff --git a/ports/esp32/plat_relo.py b/ports/esp32/plat_relo.py index 196a9f03e53c9..2af3740f8bcd6 100755 --- a/ports/esp32/plat_relo.py +++ b/ports/esp32/plat_relo.py @@ -7,8 +7,6 @@ # from C code in dynamically loaded native modules. The native module can then #include the esp-idf # header files as usual and call the functions normally. -import sys - # TODO: this needs to be loaded from a file, not hard-coded here entry_points = [ "pcnt_unit_config", diff --git a/py/nativeglue.c b/py/nativeglue.c index 068f01e7abbbb..2e0ac56ca574b 100644 --- a/py/nativeglue.c +++ b/py/nativeglue.c @@ -253,8 +253,6 @@ STATIC double mp_obj_get_float_to_d(mp_obj_t o) { #endif -extern int plat_relo_tab[]; - // these must correspond to the respective enum in nativeglue.h const mp_fun_table_t mp_fun_table = { mp_const_none, @@ -346,7 +344,6 @@ const mp_fun_table_t mp_fun_table = { &mp_stream_readinto_obj, &mp_stream_unbuffered_readline_obj, &mp_stream_write_obj, - (void*)&plat_relo_tab, }; #endif // MICROPY_EMIT_NATIVE diff --git a/py/nativeglue.h b/py/nativeglue.h index 4f0a9acf47e13..1b6d9cc7a069a 100644 --- a/py/nativeglue.h +++ b/py/nativeglue.h @@ -170,7 +170,6 @@ typedef struct _mp_fun_table_t { const mp_obj_fun_builtin_var_t *stream_readinto_obj; const mp_obj_fun_builtin_var_t *stream_unbuffered_readline_obj; const mp_obj_fun_builtin_var_t *stream_write_obj; - const int *plat_relo_tab; } mp_fun_table_t; extern const mp_fun_table_t mp_fun_table; diff --git a/tools/mpy_ld.py b/tools/mpy_ld.py index adaa26cb0afa7..cdafca47061dd 100755 --- a/tools/mpy_ld.py +++ b/tools/mpy_ld.py @@ -701,7 +701,6 @@ def link_objects(env, native_qstr_vals_len, native_qstr_objs_len): 'mp_stream_readinto_obj', 'mp_stream_unbuffered_readline_obj', 'mp_stream_write_obj', - #'plat_relo_tab', ]) } plat_relo_sec = Section('.external.plat_relo_tab', b'', 0) @@ -918,6 +917,8 @@ def build_mpy(env, entry_offset, fmpy, native_qstr_vals, native_qstr_objs): kind = 7 + kind else: # element of plat_relo_tab pass + else: + assert(0, kind) assert addr % env.arch.word_size == 0, addr offset = addr // env.arch.word_size if kind == prev_kind and base == prev_base and offset == prev_offset + 1: From 2925e4e945958feb9dc9998d27c261f5887a5eb6 Mon Sep 17 00:00:00 2001 From: Thorsten von Eicken Date: Thu, 20 Feb 2020 10:50:07 -0800 Subject: [PATCH 5/6] start auto-generating espidf module --- ports/esp32/Makefile | 1 + ports/esp32/mpconfigport.h | 7 +++ ports/esp32/plat_relo.py | 102 ++++++++++++++++++++++++++++++------- 3 files changed, 93 insertions(+), 17 deletions(-) diff --git a/ports/esp32/Makefile b/ports/esp32/Makefile index 48683a9cd27a4..4a4645ae91974 100644 --- a/ports/esp32/Makefile +++ b/ports/esp32/Makefile @@ -334,6 +334,7 @@ SRC_C = \ machine_i2c.c \ machine_pwm.c \ machine_uart.c \ + modespidf.c \ modmachine.c \ modnetwork.c \ network_lan.c \ diff --git a/ports/esp32/mpconfigport.h b/ports/esp32/mpconfigport.h index bca68538b13db..ad03449a88c08 100644 --- a/ports/esp32/mpconfigport.h +++ b/ports/esp32/mpconfigport.h @@ -162,6 +162,7 @@ #define MICROPY_PY_USOCKET_EVENTS (MICROPY_PY_WEBREPL) #define MICROPY_PY_BLUETOOTH_RANDOM_ADDR (1) #define MICROPY_PY_BLUETOOTH_DEFAULT_NAME ("ESP32") +#define MICROPY_PY_PLAT_RELO (1) // fatfs configuration #define MICROPY_FATFS_ENABLE_LFN (1) @@ -190,6 +191,11 @@ extern const struct _mp_obj_module_t mp_module_usocket; extern const struct _mp_obj_module_t mp_module_machine; extern const struct _mp_obj_module_t mp_module_network; extern const struct _mp_obj_module_t mp_module_onewire; +#if MICROPY_PY_PLAT_RELO +extern const struct _mp_obj_module_t mp_module_espidf; +#define MICROPY_PY_PLAT_RELO_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_espidf), (mp_obj_t)&mp_module_espidf }, +#endif + #define MICROPY_PORT_BUILTIN_MODULES \ { MP_OBJ_NEW_QSTR(MP_QSTR_esp), (mp_obj_t)&esp_module }, \ @@ -201,6 +207,7 @@ extern const struct _mp_obj_module_t mp_module_onewire; { MP_OBJ_NEW_QSTR(MP_QSTR_network), (mp_obj_t)&mp_module_network }, \ { MP_OBJ_NEW_QSTR(MP_QSTR__onewire), (mp_obj_t)&mp_module_onewire }, \ { MP_OBJ_NEW_QSTR(MP_QSTR_uhashlib), (mp_obj_t)&mp_module_uhashlib }, \ + MICROPY_PY_PLAT_RELO_MODULE \ #define MP_STATE_PORT MP_STATE_VM diff --git a/ports/esp32/plat_relo.py b/ports/esp32/plat_relo.py index 2af3740f8bcd6..c9d952bc29ce1 100755 --- a/ports/esp32/plat_relo.py +++ b/ports/esp32/plat_relo.py @@ -9,22 +9,90 @@ # TODO: this needs to be loaded from a file, not hard-coded here entry_points = [ - "pcnt_unit_config", - "pcnt_get_counter_value", - "pcnt_counter_pause", - "pcnt_counter_resume", - "pcnt_counter_clear", - "pcnt_intr_disable", - "mp_micropython_mem_info", + ("esp_chip_info", "P", ""), + ("esp_get_idf_version", "", "s"), + #("pcnt_unit_config", + #("pcnt_get_counter_value", + #("pcnt_counter_pause", + #("pcnt_counter_resume", + #("pcnt_counter_clear", + #("pcnt_intr_disable", ] -# Platform relocation table compiled into the MP firmware -print('\t.file\t"plat_relo.py"') -print('\t.section\t.rodata.plat_relo_tab,"a",@progbits') -print('\t.align\t4') -print('\t.global\tplat_relo_tab') -print('\t.type\tplat_relo_tab, @object') -print('plat_relo_tab:') -for ep in entry_points: - print('\t.word\t' + ep) -print('\t.size\tplat_relo_tab, .-plat_relo_tab') +imports = [ + "esp_err.h", + ] + +type_map = { "I": "uint", "i": "int", "P": "void*", "s": "char*", "": "void" } + +import sys + +if len(sys.argv) > 1 and sys.argv[1] == '--tab': + # Generate platform relocation table compiled into the MP firmware + print('\t.file\t"plat_relo.py"') + print('\t.section\t.rodata.plat_relo_tab,"a",@progbits') + print('\t.align\t4') + print('\t.global\tplat_relo_tab') + print('\t.type\tplat_relo_tab, @object') + print('plat_relo_tab:') + for ep in entry_points: + print('\t.word\t' + ep) + print('\t.size\tplat_relo_tab, .-plat_relo_tab') + +elif len(sys.argv) > 1 and sys.argv[1] == '--module': + # Generate espidf module to expose functions to python + print('''\ +#include +#include + +#include "py/runtime.h" + +#if MICROPY_PY_PLAT_RELO + +// ESP-IDF imports +''', end='') + for im in imports: + print("#include <{}>".format(im)) + for name, args, res in entry_points: + print() + print("STATIC mp_obj_t espidf_{}(".format(name), end='') + print(", ".join(["mp_obj_t arg{}".format(ix) for ix in range(len(args))]), end='') + print(") {") + for ix, fmt in enumerate(args): + print("\t// convert arg{}".format(ix)) + if fmt == "P": + print("\tmp_buffer_info_t buf{};".format(ix)) + print("\tmp_get_buffer_raise(arg{}, &buf{}, MP_BUFFER_RW);".format(ix, ix)) + print("\t// call") + if res == "": + print("\t{}(".format(name), end='') + print(", ".join([ "arg{}".format(ix) for ix in range(len(args)) ]), end='') + print(");") + print("\tmp_obj_t ret = mp_const_none;") + else: + print("\t{} ret = {}(".format(type_map[res], name)) + print("\treturn ret;\n}") + if len(args) < 4: + print("STATIC MP_DEFINE_CONST_FUN_OBJ_{}(espidf_{}_obj, espidf_{});".format( + len(args), name, name)) + else: + print("STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(espidf_{}_obj, {}, {}, espidf_{});".format( + len(args), len(args), name, name)) + print() + print("STATIC const mp_rom_map_elem_t mp_module_espidf_globals_table[] = {") + print("\t{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_espidf) },") + for name, _, _ in entry_points: + print("\t{{ MP_ROM_QSTR(MP_QSTR_{}), MP_ROM_PTR(&espidf_{}) }},".format(name, name)) + print('''\ +}; +STATIC MP_DEFINE_CONST_DICT(mp_module_espidf_globals, mp_module_espidf_globals_table); + +const mp_obj_module_t mp_module_espidf = { + .base = { &mp_type_module }, + .globals = (mp_obj_dict_t*)&mp_module_espidf_globals, +}; + +#endif +''', end='') + + From 896c38dc9af8868edfb0ba3e798b9f70953ea944 Mon Sep 17 00:00:00 2001 From: Thorsten von Eicken Date: Fri, 21 Feb 2020 21:10:29 -0800 Subject: [PATCH 6/6] added Counter device implementation in python with semi-auto-generated espidf module --- examples/natmod/esp-counter/Makefile | 35 ++++ examples/natmod/esp-counter/counter.py | 101 ++++++++++ examples/natmod/esp-counter/dyn_esp32.mk | 239 +++++++++++++++++++++++ ports/esp32/Makefile | 3 +- ports/esp32/mpconfigport.h | 5 - ports/esp32/plat_relo.py | 84 ++++---- tools/mpy_ld.py | 6 +- 7 files changed, 432 insertions(+), 41 deletions(-) create mode 100644 examples/natmod/esp-counter/Makefile create mode 100644 examples/natmod/esp-counter/counter.py create mode 100644 examples/natmod/esp-counter/dyn_esp32.mk diff --git a/examples/natmod/esp-counter/Makefile b/examples/natmod/esp-counter/Makefile new file mode 100644 index 0000000000000..19cf46b919316 --- /dev/null +++ b/examples/natmod/esp-counter/Makefile @@ -0,0 +1,35 @@ +# Location of top-level MicroPython directory +MPY_DIR = ../../.. + +# Espressif ESP-IDF path +IDF_PATH := /home/src/esp32/esp-idf-micropython +# Board to get correct ESP-IDF config +BOARD := GENERIC +# xtensa toolchain bin dir +PATH := $(IDF_PATH)/xtensa-esp32-elf/bin:$(PATH) + +# Name of module +MOD = espidf + +# Source files (.c or .py) +SRC = modespidf.c + +# Architecture to build for (x86, x64, armv7m, xtensa, xtensawin) +ARCH = xtensawin + +# Include to get the rules for compiling and linking the module +include dyn_esp32.mk +include $(MPY_DIR)/py/dynruntime.mk +CFLAGS += -std=gnu99 # override -std=c99 in dynruntime.mk +CFLAGS += -DMPY_DYN_MODULE=1 + +modespidf.c: $(MPY_DIR)/ports/esp32/plat_relo.py build/sdkconfig.h + $(ECHO) "GEN $@" + $(Q)mkdir -p build + $(Q)$< --module >$@ + +build/sdkconfig.h: $(BOARD_BUILD)/sdkconfig.h build + $(Q)cp $< $@ + +build: + $(Q)mkdir -p $@ diff --git a/examples/natmod/esp-counter/counter.py b/examples/natmod/esp-counter/counter.py new file mode 100644 index 0000000000000..02278feeb656f --- /dev/null +++ b/examples/natmod/esp-counter/counter.py @@ -0,0 +1,101 @@ +import struct, espidf + +# Directions (PCNT_COUNT_* in components/driver/include/driver/pcnt.h) +UP=1 +DOWN=2 + +# Edges +RISING=1 +FALLING=2 + +# Private constants +_COUNT_DIS = 0 +_COUNT_INC = 1 +_COUNT_DEC = 2 +_MODE_KEEP = 0 +_MODE_REVERSE = 1 +_MODE_DISABLE = 2 + +def _err_raise(err): + if err == 0: return + raise ValueError(espidf.esp_err_to_name(err)) + +class Counter: + def __init__(self, unit, pin, direction=UP, edge=RISING, limit=0, reset=True): + # check unit number + if unit < 0 or unit >= 8: + raise ValueError("unit must be in range 0..7") + self.unit = unit + # init obj + self.pin = pin + # init unit + self.init(direction, edge, limit, reset) + + def init(self, direction=UP, edge=RISING, limit=0, reset=True): + # init config elements + channel = 0 + pulse_gpio_num = self.pin + ctrl_gpio_num = -1 # not yet supported + counter_h_lim = 0 + counter_l_lim = 0 + pos_mode = _COUNT_DIS + neg_mode = _COUNT_DIS + lctrl_mode = _MODE_KEEP + hctrl_mode = _MODE_KEEP + # + self.dir_up = direction == UP + self.edges = edge + if self.dir_up: mode = _COUNT_INC + else: mode = _COUNT_DEC + # tweak config according to parameters + if edge & RISING: pos_mode = mode + if edge & FALLING: neg_mode = mode + self.limit = limit + if self.limit == 0: limit = 0x7ffff + if self.dir_up: + counter_h_lim = self.limit + else: + counter_l_lim = -self.limit + # convert pcnt_config to C struct + print('IIIIIIhhII', pulse_gpio_num, ctrl_gpio_num, + lctrl_mode, hctrl_mode, pos_mode, neg_mode, counter_h_lim, counter_l_lim, + self.unit, channel) + pcnt_config_struct = bytearray(struct.calcsize('IIIIIIhhII')) + struct.pack_into('IIIIIIhhII', pcnt_config_struct, 0, + pulse_gpio_num, ctrl_gpio_num, lctrl_mode, hctrl_mode, pos_mode, neg_mode, + counter_h_lim, counter_l_lim, self.unit, channel) + _err_raise(espidf.pcnt_unit_config(pcnt_config_struct)) + _err_raise(espidf.pcnt_counter_resume(self.unit)) # apparently it starts paused... + if reset: + _err_raise(espidf.pcnt_counter_clear(self.unit)) + + def __del__(self): + _err_raise(espidf.pcnt_counter_pause(self.unit)) + _err_raise(espidf.pcnt_intr_disable(self.unit)) + _err_raise(espidf.pcnt_set_pin(self.unit, self.channel, -1, -1)) + self.unit = -1 + self.pin = -1 + + def resume(self): + _err_raise(espidf.pcnt_unit_resume(self.unit)) + + def pause(self): + _err_raise(espidf.pcnt_unit_pause(self.unit)) + + def value(self, new_value=None): + if new_value == None: + # GET value + v_raw = bytearray(2) + _err_raise(espidf.pcnt_get_counter_value(self.unit, v_raw)) + v = struct.unpack('h', v_raw) + if not self.dir_up: + v[0] += self.limit # adjust to limit..0 interval + return v[0] + else: + # SET value + if self.dir_up and new_value != 0: + raise ValueError("only v=0 supported") + if not self.dir_up and new_value != self.limit: + raise ValueError("only v=limit supported") + _err_raise(espidf.pcnt_counter_clear(self.unit)) + return None diff --git a/examples/natmod/esp-counter/dyn_esp32.mk b/examples/natmod/esp-counter/dyn_esp32.mk new file mode 100644 index 0000000000000..14c4dfca2e43d --- /dev/null +++ b/examples/natmod/esp-counter/dyn_esp32.mk @@ -0,0 +1,239 @@ +# Select the board to build for: if not given on the command line, +# then default to GENERIC. +BOARD ?= GENERIC + +PORT_DIR := $(MPY_DIR)/ports/esp32 + +# If the build directory is not given, make it reflect the board name. +BUILD ?= build + +BOARD_DIR ?= $(PORT_DIR)/boards/$(BOARD) +BOARD_BUILD ?= $(PORT_DIR)/build-$(BOARD) +#ifeq ($(wildcard $(BOARD_DIR)/.),) +#$(error Invalid BOARD specified: $(BOARD_DIR)) +#endif +# +#include ../../py/mkenv.mk +# +## Optional (not currently used for ESP32) +#-include mpconfigport.mk +# +#ifneq ($(SDKCONFIG),) +#$(error Use the BOARD variable instead of SDKCONFIG) +#endif + +# Expected to set SDKCONFIG +include $(BOARD_DIR)/mpconfigboard.mk +SDKCONFIG := $(addprefix $(PORT_DIR)/,$(SDKCONFIG)) + +## qstr definitions (must come before including py.mk) +#QSTR_DEFS = qstrdefsport.h +#QSTR_GLOBAL_DEPENDENCIES = $(BOARD_DIR)/mpconfigboard.h +#QSTR_GLOBAL_REQUIREMENTS = $(SDKCONFIG_H) + +#MICROPY_PY_USSL = 0 +#MICROPY_SSL_AXTLS = 0 +#MICROPY_PY_BTREE = 1 +#MICROPY_VFS_FAT = 1 +#MICROPY_VFS_LFS2 = 1 + +#FROZEN_MANIFEST ?= boards/manifest.py + +## include py core make definitions +#include $(TOP)/py/py.mk + +#GIT_SUBMODULES = lib/berkeley-db-1.xx + +#PORT ?= /dev/ttyUSB0 +#BAUD ?= 460800 +#FLASH_MODE ?= dio +#FLASH_FREQ ?= 40m +#FLASH_SIZE ?= 4MB +#CROSS_COMPILE ?= xtensa-esp32-elf- +#OBJDUMP = $(CROSS_COMPILE)objdump + +#SDKCONFIG_COMBINED = $(BUILD)/sdkconfig.combined +#SDKCONFIG_H = $(BOARD_BUILD)/sdkconfig.h + +# the git hash of the currently supported ESP IDF version +ESPIDF_SUPHASH_V3 := 6ccb4cf5b7d1fdddb8c2492f9cbc926abaf230df +ESPIDF_SUPHASH_V4 := 463a9d8b7f9af8205222b80707f9bdbba7c530e1 + +define print_supported_git_hash +$(info Supported git hash (v3.3): $(ESPIDF_SUPHASH_V3)) +$(info Supported git hash (v4.0) (experimental): $(ESPIDF_SUPHASH_V4)) +endef + +# paths to ESP IDF and its components +ifeq ($(ESPIDF),) +ifneq ($(IDF_PATH),) +ESPIDF = $(IDF_PATH) +else +$(info The ESPIDF variable has not been set, please set it to the root of the esp-idf repository.) +$(info See README.md for installation instructions.) +$(call print_supported_git_hash) +$(error ESPIDF not set) +endif +endif + +ESPCOMP = $(ESPIDF)/components +#ESPTOOL ?= $(ESPCOMP)/esptool_py/esptool/esptool.py +ESPCOMP_KCONFIGS = $(shell find $(ESPCOMP) -name Kconfig) +ESPCOMP_KCONFIGS_PROJBUILD = $(shell find $(ESPCOMP) -name Kconfig.projbuild) + +# verify the ESP IDF version +ESPIDF_CURHASH := $(shell git -C $(ESPIDF) show -s --pretty=format:'%H') + +ifeq ($(ESPIDF_CURHASH),$(ESPIDF_SUPHASH_V3)) +$(info Building with ESP IDF v3) +else ifeq ($(ESPIDF_CURHASH),$(ESPIDF_SUPHASH_V4)) +$(info Building with ESP IDF v4) + +PYPARSING_VERSION = $(shell python3 -c 'import pyparsing; print(pyparsing.__version__)') +ifneq ($(PYPARSING_VERSION),2.3.1) +$(info ** ERROR **) +$(info EDP IDF requires pyparsing version less than 2.4) +$(info You will need to set up a Python virtual environment with pyparsing 2.3.1) +$(info Please see README.md for more information) +$(error Incorrect pyparsing version) +endif +else +$(info ** WARNING **) +$(info The git hash of ESP IDF does not match the supported version) +$(info The build may complete and the firmware may work but it is not guaranteed) +$(info ESP IDF path: $(ESPIDF)) +$(info Current git hash: $(ESPIDF_CURHASH)) +$(call print_supported_git_hash) +endif + +# pretty format of ESP IDF version, used internally by the IDF +IDF_VER := $(shell git -C $(ESPIDF) describe) + +#ifeq ($(shell which $(CC) 2> /dev/null),) +#$(info ** ERROR **) +#$(info Cannot find C compiler $(CC)) +#$(info Add the xtensa toolchain to your PATH. See README.md) +#$(error C compiler missing) +#endif + +## Support BLE by default when building with IDF 4.x. +## Can be explicitly disabled on the command line or board config. +#ifeq ($(ESPIDF_CURHASH),$(ESPIDF_SUPHASH_V4)) +#MICROPY_PY_BLUETOOTH ?= 1 +#ifeq ($(MICROPY_PY_BLUETOOTH),1) +#SDKCONFIG += boards/sdkconfig.ble +#MICROPY_BLUETOOTH_NIMBLE = 1 +#endif +#endif +# +# include sdkconfig to get needed configuration values +include $(SDKCONFIG) +#$(shell cp $(BOARD_BUILD)/sdkconfig* $(BUILD)) + +################################################################################ +# Compiler and linker flags + +INC_ESPCOMP += -I$(PORT_DIR) +#INC += -I$(TOP) +#INC += -I$(TOP)/lib/mp-readline +#INC += -I$(TOP)/lib/netutils +#INC += -I$(TOP)/lib/timeutils +INC_ESPCOMP += -I$(BUILD) + +INC_ESPCOMP += -I$(ESPCOMP)/bootloader_support/include +INC_ESPCOMP += -I$(ESPCOMP)/bootloader_support/include_bootloader +INC_ESPCOMP += -I$(ESPCOMP)/console +INC_ESPCOMP += -I$(ESPCOMP)/driver/include +INC_ESPCOMP += -I$(ESPCOMP)/driver/include/driver +INC_ESPCOMP += -I$(ESPCOMP)/efuse/include +INC_ESPCOMP += -I$(ESPCOMP)/efuse/esp32/include +INC_ESPCOMP += -I$(ESPCOMP)/esp32/include +INC_ESPCOMP += -I$(ESPCOMP)/espcoredump/include +INC_ESPCOMP += -I$(ESPCOMP)/soc/include +INC_ESPCOMP += -I$(ESPCOMP)/soc/esp32/include +INC_ESPCOMP += -I$(ESPCOMP)/heap/include +INC_ESPCOMP += -I$(ESPCOMP)/log/include +INC_ESPCOMP += -I$(ESPCOMP)/newlib/platform_include +INC_ESPCOMP += -I$(ESPCOMP)/newlib/include +INC_ESPCOMP += -I$(ESPCOMP)/nvs_flash/include +INC_ESPCOMP += -I$(ESPCOMP)/freertos/include +INC_ESPCOMP += -I$(ESPCOMP)/esp_ringbuf/include +INC_ESPCOMP += -I$(ESPCOMP)/esp_event/include +INC_ESPCOMP += -I$(ESPCOMP)/tcpip_adapter/include +INC_ESPCOMP += -I$(ESPCOMP)/lwip/lwip/src/include +INC_ESPCOMP += -I$(ESPCOMP)/lwip/port/esp32/include +INC_ESPCOMP += -I$(ESPCOMP)/lwip/include/apps +INC_ESPCOMP += -I$(ESPCOMP)/mbedtls/mbedtls/include +INC_ESPCOMP += -I$(ESPCOMP)/mbedtls/port/include +INC_ESPCOMP += -I$(ESPCOMP)/mdns/include +INC_ESPCOMP += -I$(ESPCOMP)/mdns/private_include +INC_ESPCOMP += -I$(ESPCOMP)/spi_flash/include +INC_ESPCOMP += -I$(ESPCOMP)/ulp/include +INC_ESPCOMP += -I$(ESPCOMP)/vfs/include +INC_ESPCOMP += -I$(ESPCOMP)/xtensa-debug-module/include +INC_ESPCOMP += -I$(ESPCOMP)/wpa_supplicant/include +INC_ESPCOMP += -I$(ESPCOMP)/wpa_supplicant/port/include +INC_ESPCOMP += -I$(ESPCOMP)/app_trace/include +INC_ESPCOMP += -I$(ESPCOMP)/app_update/include +INC_ESPCOMP += -I$(ESPCOMP)/pthread/include +INC_ESPCOMP += -I$(ESPCOMP)/smartconfig_ack/include +INC_ESPCOMP += -I$(ESPCOMP)/sdmmc/include + +ifeq ($(ESPIDF_CURHASH),$(ESPIDF_SUPHASH_V4)) +INC_ESPCOMP += -I$(ESPCOMP)/esp_common/include +INC_ESPCOMP += -I$(ESPCOMP)/esp_eth/include +INC_ESPCOMP += -I$(ESPCOMP)/esp_event/private_include +INC_ESPCOMP += -I$(ESPCOMP)/esp_rom/include +INC_ESPCOMP += -I$(ESPCOMP)/esp_wifi/include +INC_ESPCOMP += -I$(ESPCOMP)/esp_wifi/esp32/include +INC_ESPCOMP += -I$(ESPCOMP)/lwip/include/apps/sntp +INC_ESPCOMP += -I$(ESPCOMP)/spi_flash/private_include +INC_ESPCOMP += -I$(ESPCOMP)/wpa_supplicant/include/esp_supplicant +INC_ESPCOMP += -I$(ESPCOMP)/xtensa/include +INC_ESPCOMP += -I$(ESPCOMP)/xtensa/esp32/include +ifeq ($(CONFIG_BT_NIMBLE_ENABLED),y) +INC_ESPCOMP += -I$(ESPCOMP)/bt/include +INC_ESPCOMP += -I$(ESPCOMP)/bt/common/osi/include +INC_ESPCOMP += -I$(ESPCOMP)/bt/common/btc/include +INC_ESPCOMP += -I$(ESPCOMP)/bt/common/include +INC_ESPCOMP += -I$(ESPCOMP)/bt/host/nimble/nimble/porting/nimble/include +INC_ESPCOMP += -I$(ESPCOMP)/bt/host/nimble/port/include +INC_ESPCOMP += -I$(ESPCOMP)/bt/host/nimble/nimble/nimble/include +INC_ESPCOMP += -I$(ESPCOMP)/bt/host/nimble/nimble/nimble/host/include +INC_ESPCOMP += -I$(ESPCOMP)/bt/host/nimble/nimble/nimble/host/services/ans/include +INC_ESPCOMP += -I$(ESPCOMP)/bt/host/nimble/nimble/nimble/host/services/bas/include +INC_ESPCOMP += -I$(ESPCOMP)/bt/host/nimble/nimble/nimble/host/services/gap/include +INC_ESPCOMP += -I$(ESPCOMP)/bt/host/nimble/nimble/nimble/host/services/gatt/include +INC_ESPCOMP += -I$(ESPCOMP)/bt/host/nimble/nimble/nimble/host/services/ias/include +INC_ESPCOMP += -I$(ESPCOMP)/bt/host/nimble/nimble/nimble/host/services/lls/include +INC_ESPCOMP += -I$(ESPCOMP)/bt/host/nimble/nimble/nimble/host/services/tps/include +INC_ESPCOMP += -I$(ESPCOMP)/bt/host/nimble/nimble/nimble/host/util/include +INC_ESPCOMP += -I$(ESPCOMP)/bt/host/nimble/nimble/nimble/host/store/ram/include +INC_ESPCOMP += -I$(ESPCOMP)/bt/host/nimble/nimble/nimble/host/store/config/include +INC_ESPCOMP += -I$(ESPCOMP)/bt/host/nimble/nimble/porting/npl/freertos/include +INC_ESPCOMP += -I$(ESPCOMP)/bt/host/nimble/nimble/ext/tinycrypt/include +INC_ESPCOMP += -I$(ESPCOMP)/bt/host/nimble/esp-hci/include +endif +else +INC_ESPCOMP += -I$(ESPCOMP)/ethernet/include +INC_ESPCOMP += -I$(ESPCOMP)/expat/expat/expat/lib +INC_ESPCOMP += -I$(ESPCOMP)/expat/port/include +INC_ESPCOMP += -I$(ESPCOMP)/json/include +INC_ESPCOMP += -I$(ESPCOMP)/json/port/include +INC_ESPCOMP += -I$(ESPCOMP)/micro-ecc/micro-ecc +INC_ESPCOMP += -I$(ESPCOMP)/nghttp/port/include +INC_ESPCOMP += -I$(ESPCOMP)/nghttp/nghttp2/lib/includes +endif + +ifeq ($(ESPIDF_CURHASH),$(ESPIDF_SUPHASH_V4)) +ifeq ($(MICROPY_PY_BLUETOOTH),1) +CFLAGS_MOD += -DMICROPY_PY_BLUETOOTH=1 +CFLAGS_MOD += -DMICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE=1 + +ifeq ($(MICROPY_BLUETOOTH_NIMBLE),1) +CFLAGS_MOD += -DMICROPY_BLUETOOTH_NIMBLE=1 +endif +endif +endif + +CFLAGS = $(INC_ESPCOMP) $(CFLAGS_MOD) diff --git a/ports/esp32/Makefile b/ports/esp32/Makefile index 4a4645ae91974..ef826ea64e438 100644 --- a/ports/esp32/Makefile +++ b/ports/esp32/Makefile @@ -334,7 +334,6 @@ SRC_C = \ machine_i2c.c \ machine_pwm.c \ machine_uart.c \ - modespidf.c \ modmachine.c \ modnetwork.c \ network_lan.c \ @@ -784,7 +783,7 @@ $(BUILD)/application.elf: $(OBJ) $(LIB) $(BUILD)/esp32_out.ld $(BUILD)/esp32.pro $(BUILD)/plat_relo.s: plat_relo.py $(ECHO) "GEN platform relocations" - $(Q)./plat_relo.py >$@ + $(Q)./plat_relo.py --tab >$@ define compile_cxx $(ECHO) "CXX $<" diff --git a/ports/esp32/mpconfigport.h b/ports/esp32/mpconfigport.h index ad03449a88c08..236866f9e4a33 100644 --- a/ports/esp32/mpconfigport.h +++ b/ports/esp32/mpconfigport.h @@ -191,10 +191,6 @@ extern const struct _mp_obj_module_t mp_module_usocket; extern const struct _mp_obj_module_t mp_module_machine; extern const struct _mp_obj_module_t mp_module_network; extern const struct _mp_obj_module_t mp_module_onewire; -#if MICROPY_PY_PLAT_RELO -extern const struct _mp_obj_module_t mp_module_espidf; -#define MICROPY_PY_PLAT_RELO_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_espidf), (mp_obj_t)&mp_module_espidf }, -#endif #define MICROPY_PORT_BUILTIN_MODULES \ @@ -207,7 +203,6 @@ extern const struct _mp_obj_module_t mp_module_espidf; { MP_OBJ_NEW_QSTR(MP_QSTR_network), (mp_obj_t)&mp_module_network }, \ { MP_OBJ_NEW_QSTR(MP_QSTR__onewire), (mp_obj_t)&mp_module_onewire }, \ { MP_OBJ_NEW_QSTR(MP_QSTR_uhashlib), (mp_obj_t)&mp_module_uhashlib }, \ - MICROPY_PY_PLAT_RELO_MODULE \ #define MP_STATE_PORT MP_STATE_VM diff --git a/ports/esp32/plat_relo.py b/ports/esp32/plat_relo.py index c9d952bc29ce1..398a500ecf89f 100755 --- a/ports/esp32/plat_relo.py +++ b/ports/esp32/plat_relo.py @@ -11,19 +11,26 @@ entry_points = [ ("esp_chip_info", "P", ""), ("esp_get_idf_version", "", "s"), - #("pcnt_unit_config", - #("pcnt_get_counter_value", - #("pcnt_counter_pause", - #("pcnt_counter_resume", - #("pcnt_counter_clear", - #("pcnt_intr_disable", + ("esp_err_to_name", "I", "s"), + ("pcnt_unit_config", "P", "I"), + ("pcnt_get_counter_value", "IP", "I"), + ("pcnt_counter_pause", "I", "I"), + ("pcnt_counter_resume", "I", "I"), + ("pcnt_counter_clear", "I", "I"), + ("pcnt_intr_disable", "I", "I"), ] imports = [ "esp_err.h", + "esp_idf_version.h", + "esp_system.h", + "driver/pcnt.h", ] -type_map = { "I": "uint", "i": "int", "P": "void*", "s": "char*", "": "void" } +type_map = { "I": "uint", "i": "int", "H": "uint16", "h": "int16", "B": "uint8", "b": "int8", + "P": "void*", "s": "char*", "": "void" } + +return_map = { "I": "mp_obj_new_int({})", "s": "mp_obj_new_str({0}, strlen({0}))", "": "{}" } import sys @@ -35,22 +42,21 @@ print('\t.global\tplat_relo_tab') print('\t.type\tplat_relo_tab, @object') print('plat_relo_tab:') - for ep in entry_points: - print('\t.word\t' + ep) + for name, _, _ in entry_points: + print('\t.word\t' + name) print('\t.size\tplat_relo_tab, .-plat_relo_tab') -elif len(sys.argv) > 1 and sys.argv[1] == '--module': +elif len(sys.argv) > 1 and (sys.argv[1] == '--module' or sys.argv[1] == '--builtin'): # Generate espidf module to expose functions to python - print('''\ -#include -#include - -#include "py/runtime.h" + builtin = sys.argv[1] == '--builtin' + print('#include ') + print('#include ') + if builtin: + print('#include "py/runtime.h"') + else: + print('#include "py/dynruntime.h"') -#if MICROPY_PY_PLAT_RELO - -// ESP-IDF imports -''', end='') + print('// ESP-IDF imports') for im in imports: print("#include <{}>".format(im)) for name, args, res in entry_points: @@ -61,17 +67,21 @@ for ix, fmt in enumerate(args): print("\t// convert arg{}".format(ix)) if fmt == "P": - print("\tmp_buffer_info_t buf{};".format(ix)) - print("\tmp_get_buffer_raise(arg{}, &buf{}, MP_BUFFER_RW);".format(ix, ix)) + print("\tmp_buffer_info_t val{}_buf;".format(ix)) + print("\tmp_get_buffer_raise(arg{0}, &val{0}_buf, MP_BUFFER_RW);".format(ix)) + print("\tvoid *val{0} = (void *)(val{0}_buf.buf);".format(ix)) + + elif fmt in frozenset(["I", "i", "H", "h", "B", "b"]): + print("\t{1} val{0} = ({1})mp_obj_get_int(arg{0});".format(ix, type_map[fmt])) print("\t// call") if res == "": - print("\t{}(".format(name), end='') - print(", ".join([ "arg{}".format(ix) for ix in range(len(args)) ]), end='') - print(");") print("\tmp_obj_t ret = mp_const_none;") + print("\t{}(".format(name), end='') else: - print("\t{} ret = {}(".format(type_map[res], name)) - print("\treturn ret;\n}") + print("\tconst {} ret = {}(".format(type_map[res], name), end='') + print(", ".join([ "val{}".format(ix) for ix in range(len(args)) ]), end='') + print(");") + print("\treturn " + return_map[res].format('ret') + ";\n}") if len(args) < 4: print("STATIC MP_DEFINE_CONST_FUN_OBJ_{}(espidf_{}_obj, espidf_{});".format( len(args), name, name)) @@ -79,11 +89,13 @@ print("STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(espidf_{}_obj, {}, {}, espidf_{});".format( len(args), len(args), name, name)) print() - print("STATIC const mp_rom_map_elem_t mp_module_espidf_globals_table[] = {") - print("\t{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_espidf) },") - for name, _, _ in entry_points: - print("\t{{ MP_ROM_QSTR(MP_QSTR_{}), MP_ROM_PTR(&espidf_{}) }},".format(name, name)) - print('''\ + if sys.argv[1] == '--builtin': + # generate module built into the firmware + print("STATIC const mp_rom_map_elem_t mp_module_espidf_globals_table[] = {") + print("\t{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_espidf) },") + for name, _, _ in entry_points: + print("\t{{ MP_ROM_QSTR(MP_QSTR_{}), MP_ROM_PTR(&espidf_{}) }},".format(name, name)) + print('''\ }; STATIC MP_DEFINE_CONST_DICT(mp_module_espidf_globals, mp_module_espidf_globals_table); @@ -94,5 +106,11 @@ #endif ''', end='') - - + else: + # generate dynamically loadable module + print("mp_obj_t mpy_init(mp_obj_fun_bc_t *self, size_t n_args, size_t n_kw, mp_obj_t *args) {") + print("\tMP_DYNRUNTIME_INIT_ENTRY") + for name, _, _ in entry_points: + print("\tmp_store_global(MP_QSTR_{0}, MP_OBJ_FROM_PTR(&espidf_{0}_obj));".format(name)) + print("\tMP_DYNRUNTIME_INIT_EXIT") + print("}") diff --git a/tools/mpy_ld.py b/tools/mpy_ld.py index cdafca47061dd..bd11b164c0ff3 100755 --- a/tools/mpy_ld.py +++ b/tools/mpy_ld.py @@ -40,6 +40,9 @@ # TODO: this needs to be loaded from a file, not hard-coded here. plat_relo_entries = { name : idx for idx, name in enumerate([ + "esp_chip_info", + "esp_get_idf_version", + "esp_err_to_name", "pcnt_unit_config", "pcnt_get_counter_value", "pcnt_counter_pause", @@ -47,6 +50,7 @@ "pcnt_counter_clear", "pcnt_intr_disable", "mp_micropython_mem_info", + "strlen", ]) } @@ -918,7 +922,7 @@ def build_mpy(env, entry_offset, fmpy, native_qstr_vals, native_qstr_objs): else: # element of plat_relo_tab pass else: - assert(0, kind) + assert 0, kind assert addr % env.arch.word_size == 0, addr offset = addr // env.arch.word_size if kind == prev_kind and base == prev_base and offset == prev_offset + 1: