Skip to content

esp32: proof of concept for stubs to call platform-dependent functions #5653

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions examples/natmod/esp-counter/Makefile
Original file line number Diff line number Diff line change
@@ -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 $@
101 changes: 101 additions & 0 deletions examples/natmod/esp-counter/counter.py
Original file line number Diff line number Diff line change
@@ -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
239 changes: 239 additions & 0 deletions examples/natmod/esp-counter/dyn_esp32.mk
Original file line number Diff line number Diff line change
@@ -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)
Loading
pFad - Phonifier reborn

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

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


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy