diff --git a/Sming/Arch/Esp32/Components/driver/uart.cpp b/Sming/Arch/Esp32/Components/driver/uart.cpp index 34752c9d76..a5182fbca7 100644 --- a/Sming/Arch/Esp32/Components/driver/uart.cpp +++ b/Sming/Arch/Esp32/Components/driver/uart.cpp @@ -132,11 +132,6 @@ void notify(smg_uart_t* uart, smg_uart_notify_code_t code) } } -__forceinline bool uart_isr_enabled(uint8_t nr) -{ - return bitRead(isrMask, nr); -} - /** @brief Determine if the given uart is a real uart or a virtual one */ __forceinline bool is_physical(int uart_nr) diff --git a/Sming/Arch/Esp32/Components/esp32/component.mk b/Sming/Arch/Esp32/Components/esp32/component.mk index 002f489da1..29893fb9ad 100644 --- a/Sming/Arch/Esp32/Components/esp32/component.mk +++ b/Sming/Arch/Esp32/Components/esp32/component.mk @@ -24,12 +24,15 @@ SDK_LIBDIRS := \ $(ESP_VARIANT)/ld \ esp_rom/$(ESP_VARIANT)/ld +ESP32_COMPONENT_PATH := $(COMPONENT_PATH) +SDK_DEFAULT_PATH := $(ESP32_COMPONENT_PATH)/sdk + LIBDIRS += \ $(SDK_COMPONENT_LIBDIR) \ $(SDK_BUILD_BASE)/esp-idf/mbedtls/mbedtls/library \ $(SDK_BUILD_BASE)/esp-idf/$(ESP_VARIANT) \ $(SDK_BUILD_BASE)/esp-idf/$(ESP_VARIANT)/ld \ - $(COMPONENT_PATH)/ld \ + $(ESP32_COMPONENT_PATH)/ld \ $(addprefix $(SDK_COMPONENTS_PATH)/,$(SDK_LIBDIRS)) SDK_INCDIRS := \ @@ -151,11 +154,6 @@ ifeq ($(ESP_VARIANT),esp32) SDK_ESP_WIFI_LIBS += rtc endif -SDK_NEWLIB_LIBS := \ - c \ - m \ - stdc++ - ifdef IDF_TARGET_ARCH_RISCV SDK_TARGET_ARCH_LIBS := hal else @@ -163,9 +161,7 @@ SDK_TARGET_ARCH_LIBS := hal xt_hal endif EXTRA_LIBS := \ - gcc \ $(SDK_COMPONENTS) \ - $(SDK_NEWLIB_LIBS) \ $(SDK_TARGET_ARCH_LIBS) ifneq ($(DISABLE_WIFI),1) @@ -196,23 +192,18 @@ LDFLAGS_esp32s3 := \ $(call LinkerScript,rom.newlib-data) \ $(call LinkerScript,rom.spiflash) +SDK_WRAP_SYMBOLS := +SDK_UNDEF_SYMBOLS := + +$(foreach c,$(wildcard $(SDK_DEFAULT_PATH)/*.mk),$(eval include $c)) + EXTRA_LDFLAGS := \ - -u esp_app_desc \ - -u __cxa_guard_dummy -u __cxx_fatal_exception \ -T $(ESP_VARIANT)_out.ld \ - -u ld_include_panic_highint_hdl \ $(call LinkerScript,project) \ $(call LinkerScript,peripherals) \ $(call LinkerScript,rom) \ $(call LinkerScript,rom.api) \ $(call LinkerScript,rom.libgcc) \ - -u newlib_include_locks_impl \ - -u newlib_include_heap_impl \ - -u newlib_include_syscalls_impl \ - -u pthread_include_pthread_impl \ - -u pthread_include_pthread_cond_impl \ - -u pthread_include_pthread_local_storage_impl \ - -Wl,--undefined=uxTopUsedPriority \ $(call Wrap,\ esp_event_loop_create_default \ esp_event_handler_register \ @@ -221,10 +212,12 @@ EXTRA_LDFLAGS := \ esp_event_handler_instance_unregister \ esp_event_post \ esp_event_isr_post) \ - $(LDFLAGS_$(ESP_VARIANT)) + $(LDFLAGS_$(ESP_VARIANT)) \ + $(call Undef,$(SDK_UNDEF_SYMBOLS)) \ + $(call Wrap,$(SDK_WRAP_SYMBOLS)) + -SDK_DEFAULT_PATH := $(COMPONENT_PATH)/sdk -SDK_PROJECT_PATH := $(COMPONENT_PATH)/project/$(ESP_VARIANT)/$(BUILD_TYPE) +SDK_PROJECT_PATH := $(ESP32_COMPONENT_PATH)/project/$(ESP_VARIANT)/$(BUILD_TYPE) SDK_CONFIG_DEFAULTS := $(SDK_PROJECT_PATH)/sdkconfig.defaults SDKCONFIG_MAKEFILE := $(SDK_PROJECT_PATH)/sdkconfig @@ -253,7 +246,7 @@ CUSTOM_TARGETS += checksdk .PHONY: checksdk checksdk: $(SDK_PROJECT_PATH) $(SDKCONFIG_H) $(SDKCONFIG_MAKEFILE) $(Q) $(NINJA) -C $(SDK_BUILD_BASE) bootloader app - $(Q) $(MAKE) --no-print-directory -C $(SDK_DEFAULT_PATH) -f misc.mk copylibs + $(Q) $(MAKE) --no-print-directory -C $(ESP32_COMPONENT_PATH) -f misc.mk copylibs $(SDKCONFIG_H) $(SDKCONFIG_MAKEFILE) $(SDK_COMPONENT_LIBS): $(SDK_PROJECT_PATH) $(SDK_CONFIG_DEFAULTS) | $(SDK_BUILD_BASE) $(SDK_COMPONENT_LIBDIR) $(Q) $(SDK_BUILD) reconfigure @@ -312,4 +305,4 @@ sdk: ##Pass options to IDF builder, e.g. `make sdk -- --help` or `make sdk menuc .PHONY: checkdirs checkdirs: | checksdk - + diff --git a/Sming/Arch/Esp32/Components/esp32/sdk/misc.mk b/Sming/Arch/Esp32/Components/esp32/misc.mk similarity index 100% rename from Sming/Arch/Esp32/Components/esp32/sdk/misc.mk rename to Sming/Arch/Esp32/Components/esp32/misc.mk diff --git a/Sming/Arch/Esp32/Components/esp32/sdk/app_update.mk b/Sming/Arch/Esp32/Components/esp32/sdk/app_update.mk new file mode 100644 index 0000000000..c70fd37ebe --- /dev/null +++ b/Sming/Arch/Esp32/Components/esp32/sdk/app_update.mk @@ -0,0 +1,7 @@ +# +# app_update +# + +# esp_app_desc structure is added as an undefined symbol because otherwise the +# linker will ignore this structure as it has no other files depending on it. +SDK_UNDEF_SYMBOLS += esp_app_desc diff --git a/Sming/Arch/Esp32/Components/esp32/sdk/cxx.mk b/Sming/Arch/Esp32/Components/esp32/sdk/cxx.mk new file mode 100644 index 0000000000..cd150b4cb6 --- /dev/null +++ b/Sming/Arch/Esp32/Components/esp32/sdk/cxx.mk @@ -0,0 +1,57 @@ +# +# cxx +# + +ifndef CONFIG_COMPILER_CXX_EXCEPTIONS +# If exceptions are disabled, ensure our fatal exception +# hooks are preferentially linked over libstdc++ which +# has full exception support +SDK_WRAP_SYMBOLS += \ + _Unwind_SetEnableExceptionFdeSorting \ + __register_frame_info_bases \ + __register_frame_info \ + __register_frame \ + __register_frame_info_table_bases \ + __register_frame_info_table \ + __register_frame_table \ + __deregister_frame_info_bases \ + __deregister_frame_info \ + _Unwind_Find_FDE \ + _Unwind_GetGR \ + _Unwind_GetCFA \ + _Unwind_GetIP \ + _Unwind_GetIPInfo \ + _Unwind_GetRegionStart \ + _Unwind_GetDataRelBase \ + _Unwind_GetTextRelBase \ + _Unwind_SetIP \ + _Unwind_SetGR \ + _Unwind_GetLanguageSpecificData \ + _Unwind_FindEnclosingFunction \ + _Unwind_Resume \ + _Unwind_RaiseException \ + _Unwind_DeleteException \ + _Unwind_ForcedUnwind \ + _Unwind_Resume_or_Rethrow \ + _Unwind_Backtrace \ + __cxa_call_unexpected \ + __gxx_personality_v0 + +SDK_UNDEF_SYMBOLS += __cxx_fatal_exception +endif + +SDK_UNDEF_SYMBOLS += __cxa_guard_dummy + +# Force libpthread to appear later than libstdc++ in link line since libstdc++ depends on libpthread. +# Furthermore, force libcxx to appear later than libgcc because some libgcc unwind code is wrapped, if C++ +# exceptions are disabled. libcxx (this component) provides the unwind code wrappers. +# This is to prevent linking of libgcc's unwind code which considerably increases the binary size. + +# idf_component_get_property(pthread pthread COMPONENT_LIB) +# idf_component_get_property(cxx cxx COMPONENT_LIB) +# add_library(stdcpp_pthread INTERFACE) +# target_link_libraries(stdcpp_pthread INTERFACE stdc++ $) +# target_link_libraries(${COMPONENT_LIB} PUBLIC stdcpp_pthread) +# add_library(libgcc_cxx INTERFACE) +# target_link_libraries(libgcc_cxx INTERFACE gcc $) +# target_link_libraries(${COMPONENT_LIB} PUBLIC libgcc_cxx) diff --git a/Sming/Arch/Esp32/Components/esp32/sdk/esp32.mk b/Sming/Arch/Esp32/Components/esp32/sdk/esp32.mk new file mode 100644 index 0000000000..84ac656dd0 --- /dev/null +++ b/Sming/Arch/Esp32/Components/esp32/sdk/esp32.mk @@ -0,0 +1,5 @@ +# +# esp32 +# + +SDK_UNDEF_SYMBOLS += call_user_start_cpu0 diff --git a/Sming/Arch/Esp32/Components/esp32/sdk/esp_rom.mk b/Sming/Arch/Esp32/Components/esp32/sdk/esp_rom.mk new file mode 100644 index 0000000000..ed4f2b64c5 --- /dev/null +++ b/Sming/Arch/Esp32/Components/esp32/sdk/esp_rom.mk @@ -0,0 +1,6 @@ +# +# esp_rom +# +ifdef CONFIG_IDF_TARGET_ARCH_XTENSA +SDK_WRAP_SYMBOLS += longjmp +endif diff --git a/Sming/Arch/Esp32/Components/esp32/sdk/esp_system.mk b/Sming/Arch/Esp32/Components/esp32/sdk/esp_system.mk new file mode 100644 index 0000000000..1d7f1f8726 --- /dev/null +++ b/Sming/Arch/Esp32/Components/esp32/sdk/esp_system.mk @@ -0,0 +1,18 @@ +# +# esp_system +# + +# After system initialization, `start_app` (and its other cores variant) is called. +# This is provided by the user or from another component. Since we can't establish +# dependency on what we don't know, force linker to not drop the symbol regardless +# of link line order. +SDK_UNDEF_SYMBOLS += start_app + +ifndef CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE +SDK_UNDEF_SYMBOLS += start_app_other_cores +endif + +# ld_include_panic_highint_hdl is added as an undefined symbol because otherwise the +# linker will ignore panic_highint_hdl.S as it has no other files depending on any +# symbols in it. +SDK_UNDEF_SYMBOLS += ld_include_panic_highint_hdl diff --git a/Sming/Arch/Esp32/Components/esp32/sdk/freertos.mk b/Sming/Arch/Esp32/Components/esp32/sdk/freertos.mk new file mode 100644 index 0000000000..8d658211a5 --- /dev/null +++ b/Sming/Arch/Esp32/Components/esp32/sdk/freertos.mk @@ -0,0 +1,7 @@ +# +# freertos +# + +ifdef CONFIG_FREERTOS_DEBUG_OCDAWARE +EXTRA_LDFLAGS += -Wl,--undefined=uxTopUsedPriority +endif diff --git a/Sming/Arch/Esp32/Components/esp32/sdk/heap.mk b/Sming/Arch/Esp32/Components/esp32/sdk/heap.mk new file mode 100644 index 0000000000..b16d826602 --- /dev/null +++ b/Sming/Arch/Esp32/Components/esp32/sdk/heap.mk @@ -0,0 +1,16 @@ +# +# heap +# + +ifdef CONFIG_HEAP_TRACING +SDK_WRAP_SYMBOLS += \ + calloc \ + malloc \ + free \ + realloc \ + heap_caps_malloc \ + heap_caps_free \ + heap_caps_realloc \ + heap_caps_malloc_default \ + heap_caps_realloc_default +endif diff --git a/Sming/Arch/Esp32/Components/esp32/sdk/newlib.mk b/Sming/Arch/Esp32/Components/esp32/sdk/newlib.mk new file mode 100644 index 0000000000..75613e5733 --- /dev/null +++ b/Sming/Arch/Esp32/Components/esp32/sdk/newlib.mk @@ -0,0 +1,13 @@ +# +# newlib +# + +# Forces the linker to include heap, syscall, pthread and retargetable locks from this component, +# instead of the implementations provided by newlib. +SDK_UNDEF_SYMBOLS += \ + newlib_include_heap_impl \ + newlib_include_syscalls_impl \ + newlib_include_pthread_impl + +# Must link before standard C library +LIBS := newlib $(LIBS) diff --git a/Sming/Arch/Esp32/Components/esp32/sdk/pthread.mk b/Sming/Arch/Esp32/Components/esp32/sdk/pthread.mk new file mode 100644 index 0000000000..c24e62cb1e --- /dev/null +++ b/Sming/Arch/Esp32/Components/esp32/sdk/pthread.mk @@ -0,0 +1,11 @@ +# +# pthread +# +SDK_UNDEF_SYMBOLS += \ + pthread_include_pthread_impl \ + pthread_include_pthread_cond_impl \ + pthread_include_pthread_local_storage_impl + +ifdef CONFIG_FREERTOS_ENABLE_STATIC_TASK_CLEAN_UP +SDK_WRAP_SYMBOLS += vPortCleanUpTCB +endif diff --git a/Sming/Arch/Esp32/Components/esp32/src/include/esp_systemapi.h b/Sming/Arch/Esp32/Components/esp32/src/include/esp_systemapi.h index 89482e7071..9b81061be4 100644 --- a/Sming/Arch/Esp32/Components/esp32/src/include/esp_systemapi.h +++ b/Sming/Arch/Esp32/Components/esp32/src/include/esp_systemapi.h @@ -10,11 +10,6 @@ #pragma once -// ==== taken from ESP8266/ets_sys.h -#define ETS_GPIO_INUM 4 - -// ==== - // Default types #include #include @@ -38,7 +33,7 @@ #include #include -#define __ESP32_EX__ // System definition ESP8266 SOC +#define __ESP32_EX__ // System definition ESP32 SOC #define LOCAL static diff --git a/Sming/Arch/Esp32/Components/libc/component.mk b/Sming/Arch/Esp32/Components/libc/component.mk index 3980983c81..065d909a37 100644 --- a/Sming/Arch/Esp32/Components/libc/component.mk +++ b/Sming/Arch/Esp32/Components/libc/component.mk @@ -3,4 +3,21 @@ COMPONENT_INCDIRS := src/include COMPONENT_DOXYGEN_INPUT := src/include/sys -EXTRA_LDFLAGS := $(call Wrap,_write_r _read_r) +LIBC_WRAPSYMS := \ + _write_r \ + _read_r \ + putchar \ + puts \ + vprintf \ + printf \ + vsnprintf \ + vsprintf \ + sprintf + +EXTRA_LDFLAGS := $(call Wrap,$(LIBC_WRAPSYMS)) + +EXTRA_LIBS := \ + c \ + m \ + gcc \ + stdc++ diff --git a/Sming/Arch/Esp32/Components/libc/src/replacements.c b/Sming/Arch/Esp32/Components/libc/src/replacements.c index 04d972852b..768f09ee11 100644 --- a/Sming/Arch/Esp32/Components/libc/src/replacements.c +++ b/Sming/Arch/Esp32/Components/libc/src/replacements.c @@ -6,14 +6,17 @@ #include #include -ssize_t __wrap__write_r(struct _reent* r, int fd, const void* data, size_t size) +#define BUFMAX 16384 +#define WRAP(x) __wrap_##x + +ssize_t WRAP(_write_r)(struct _reent* r, int fd, const void* data, size_t size) { (void)r; (void)fd; // Ignore, direct everything return m_nputs(data, size); } -ssize_t __wrap__read_r(struct _reent* r, int fd, void* dst, size_t size) +ssize_t WRAP(_read_r)(struct _reent* r, int fd, void* dst, size_t size) { (void)r; (void)fd; @@ -22,3 +25,57 @@ ssize_t __wrap__read_r(struct _reent* r, int fd, void* dst, size_t size) errno = ENOSYS; return -1; } + +size_t WRAP(putc)(char c) +{ + return m_putc(c); +} + +size_t WRAP(puts)(const char* str) +{ + return m_puts(str); +} + +int WRAP(vprintf)(const char* format, va_list arg) +{ + return m_vprintf(format, arg); +} + +int WRAP(printf)(char const* fmt, ...) +{ + va_list args; + va_start(args, fmt); + int n = m_vprintf(fmt, args); + va_end(args); + return n; +} + +int WRAP(vsnprintf)(char* buf, size_t maxLen, const char* fmt, va_list args) +{ + return m_vsnprintf(buf, maxLen, fmt, args); +} + +int WRAP(snprintf)(char* buf, int length, const char* fmt, ...) +{ + va_list args; + + va_start(args, fmt); + int n = m_vsnprintf(buf, length, fmt, args); + va_end(args); + + return n; +} + +int WRAP(vsprintf)(char* buf, const char* fmt, va_list args) +{ + return m_vsnprintf(buf, BUFMAX, fmt, args); +} + +int WRAP(sprintf)(char* buf, const char* fmt, ...) +{ + va_list args; + va_start(args, fmt); + int n = m_vsnprintf(buf, BUFMAX, fmt, args); + va_end(args); + return n; +} diff --git a/Sming/Arch/Esp32/Components/spi_flash/flashmem.cpp b/Sming/Arch/Esp32/Components/spi_flash/flashmem.cpp index 76f01d16c0..79a2f02f94 100644 --- a/Sming/Arch/Esp32/Components/spi_flash/flashmem.cpp +++ b/Sming/Arch/Esp32/Components/spi_flash/flashmem.cpp @@ -39,7 +39,7 @@ uint32_t flashmem_read(void* to, uint32_t fromaddr, uint32_t size) bool flashmem_erase_sector(uint32_t sector_id) { - debug_e("flashmem_erase_sector(0x%08x)", sector_id); + debug_d("flashmem_erase_sector(0x%08x)", sector_id); return spi_flash_erase_sector(sector_id) == SPI_FLASH_RESULT_OK; } diff --git a/Sming/Arch/Esp32/Components/spi_flash/include/esp_spi_flash.h b/Sming/Arch/Esp32/Components/spi_flash/include/esp_spi_flash.h index f4b34017ef..331a9ddc5d 100644 --- a/Sming/Arch/Esp32/Components/spi_flash/include/esp_spi_flash.h +++ b/Sming/Arch/Esp32/Components/spi_flash/include/esp_spi_flash.h @@ -151,10 +151,13 @@ uint32_t flashmem_find_sector(uint32_t address, uint32_t* pstart, uint32_t* pend */ uint32_t flashmem_get_sector_of_address(uint32_t addr); -/** @} */ - +/* + * @brief Get unique 32-bit flash identification code + */ uint32_t spi_flash_get_id(void); +/** @} */ + #ifdef __cplusplus } #endif diff --git a/Sming/Arch/Esp32/Components/spi_flash/include/iram_precache.h b/Sming/Arch/Esp32/Components/spi_flash/include/iram_precache.h index ecebfa0ca2..e48a605ba0 100644 --- a/Sming/Arch/Esp32/Components/spi_flash/include/iram_precache.h +++ b/Sming/Arch/Esp32/Components/spi_flash/include/iram_precache.h @@ -2,8 +2,6 @@ * Dummy stub. ESP32 has tons of IRAM. */ -#include - #pragma once /** @@ -16,22 +14,4 @@ #define IRAM_PRECACHE_START(tag) #define IRAM_PRECACHE_END(tag) -#ifdef __cplusplus -extern "C" { -#endif - -/** - * @brief Pre-load flash data into the flash instruction cache - * @param addr First location to cache, specify NULL to use current location. - * @param bytes Number of bytes to cache - * @note All pages containing the requested region will be read to pull them into cache RAM. - */ -inline void iram_precache(void* addr, uint32_t bytes) -{ -} - /** @} */ - -#ifdef __cplusplus -} -#endif diff --git a/Sming/Arch/Esp32/app.mk b/Sming/Arch/Esp32/app.mk index db6ca31cbf..a2ffcaa001 100644 --- a/Sming/Arch/Esp32/app.mk +++ b/Sming/Arch/Esp32/app.mk @@ -16,24 +16,8 @@ application: $(TARGET_BIN) $(TARGET_OUT): $(COMPONENTS_AR) $(info $(notdir $(PROJECT_DIR)): Linking $@) $(Q) $(LD) $(addprefix -L,$(LIBDIRS)) $(LDFLAGS) -Wl,--start-group $(COMPONENTS_AR) $(addprefix -l,$(LIBS)) -Wl,--end-group -o $@ - - $(Q) $(MEMANALYZER) $@ > $(FW_MEMINFO_NEW) - - $(Q) if [ -f "$(FW_MEMINFO_NEW)" -a -f "$(FW_MEMINFO_OLD)" ]; then \ - $(AWK) -F "|" ' \ - FILENAME == "$(FW_MEMINFO_OLD)" { \ - arr[$$1]=$$5 \ - } \ - FILENAME == "$(FW_MEMINFO_NEW)" { \ - if (arr[$$1] != $$5) { \ - printf "%s%s%+d%s", substr($$0, 1, length($$0) - 1)," (",$$5 - arr[$$1],")\n" \ - } else { \ - print $$0 \ - } \ - }' $(FW_MEMINFO_OLD) $(FW_MEMINFO_NEW); \ - elif [ -f "$(FW_MEMINFO_NEW)" ]; then \ - cat $(FW_MEMINFO_NEW); \ - fi + $(Q) $(MEMANALYZER) $@ > $(FW_MEMINFO) + $(Q) cat $(FW_MEMINFO) CHIP_REV_MIN := $(CONFIG_$(call ToUpper,$(ESP_VARIANT))_REV_MIN) ifeq ($(CHIP_REV_MIN),) diff --git a/Sming/Arch/Esp32/build.mk b/Sming/Arch/Esp32/build.mk index 3356671ea9..0092030400 100644 --- a/Sming/Arch/Esp32/build.mk +++ b/Sming/Arch/Esp32/build.mk @@ -131,174 +131,40 @@ endif IDF_VER := $(shell echo "$(IDF_VER_T)" | cut -c 1-31) # [ Sming specific flags ] -DEBUG_VARS += IDF_PATH IDF_VER +DEBUG_VARS += IDF_PATH IDF_VER -# Set default LDFLAGS -EXTRA_LDFLAGS ?= -LDFLAGS ?= -nostdlib \ - -u call_user_start_cpu0 \ - $(EXTRA_LDFLAGS) \ - -Wl,--gc-sections \ - -Wl,-static \ - -Wl,--start-group \ - $(COMPONENT_LDFLAGS) \ - -lgcc \ - -lstdc++ \ - -lgcov \ - -Wl,--end-group \ - -Wl,-EL - -SMING_C_STD := gnu99 +# IDF uses 'asm' keyword +SMING_C_STD := gnu11 -# Set default CPPFLAGS, CFLAGS, CXXFLAGS -# These are exported so that components can use them when compiling. -# If you need your component to add CFLAGS/etc for it's own source compilation only, set CFLAGS += in your component's Makefile. -# If you need your component to add CFLAGS/etc globally for all source -# files, set CFLAGS += in your component's Makefile.projbuild -# If you need to set CFLAGS/CPPFLAGS/CXXFLAGS at project level, set them in application Makefile -# before including project.mk. Default flags will be added before the ones provided in application Makefile. - -# This variable stores the common C/C++ flags -# CPPFLAGS used by C preprocessor -# If any flags are defined in application Makefile, add them at the end. +# Common C/C++ flags CPPFLAGS += \ -DESP_PLATFORM \ - -D IDF_VER=\"$(IDF_VER)\" \ + -DIDF_VER=\"$(IDF_VER)\" \ -MMD \ -MP \ - $(EXTRA_CPPFLAGS) - -# Sming specific CPPFLAGS -CPPFLAGS += \ + $(EXTRA_CPPFLAGS) \ -DARCH_ESP32 \ -D__ets__ \ - -DICACHE_FLASH \ - -DUSE_OPTIMIZE_PRINTF \ - -DESP32 + -D_GNU_SOURCE \ + -DCONFIG_NONE_OS PROJECT_VER ?= export IDF_VER export PROJECT_NAME export PROJECT_VER -# Warnings-related flags relevant both for C and C++ -COMMON_WARNING_FLAGS := -Wall -Werror=all \ - -Wno-error=unused-function \ - -Wno-error=unused-but-set-variable \ - -Wno-error=unused-variable \ - -Wno-error=deprecated-declarations \ - -Wno-error=extra \ - -Wno-unused-parameter -Wno-error=sign-compare \ - -Wno-error=ignored-qualifiers \ - -Wno-error=missing-field-initializers \ - -Wno-error=implicit-fallthrough - -# Sming warning.... -COMMON_WARNING_FLAGS += -Wno-error=undef - -ifdef CONFIG_COMPILER_DISABLE_GCC8_WARNINGS -COMMON_WARNING_FLAGS += -Wno-parentheses \ - -Wno-sizeof-pointer-memaccess \ - -Wno-clobbered \ - -Wno-format-overflow \ - -Wno-stringop-truncation \ - -Wno-misleading-indentation \ - -Wno-cast-function-type \ - -Wno-implicit-fallthrough \ - -Wno-unused-const-variable \ - -Wno-switch-unreachable \ - -Wno-format-truncation \ - -Wno-memset-elt-size \ - -Wno-int-in-bool-context -endif - -ifdef CONFIG_COMPILER_WARN_WRITE_STRINGS -COMMON_WARNING_FLAGS += -Wwrite-strings -endif #CONFIG_COMPILER_WARN_WRITE_STRINGS - # Flags which control code generation and dependency generation, both for C and C++ -COMMON_FLAGS := \ - -Wno-frame-address \ - -ffunction-sections -fdata-sections \ - -fstrict-volatile-bitfields \ - -nostdlib +CPPFLAGS += -Wno-frame-address -ifdef IDF_TARGET_ARCH_RISCV -COMMON_FLAGS += -DIDF_TARGET_ARCH_RISCV=1 -else -COMMON_FLAGS += \ +ifndef IDF_TARGET_ARCH_RISCV +CPPFLAGS += \ -mlongcalls \ -mtext-section-literals endif -ifndef IS_BOOTLOADER_BUILD -# stack protection (only one option can be selected in menuconfig) -ifdef CONFIG_COMPILER_STACK_CHECK_MODE_NORM -COMMON_FLAGS += -fstack-protector -endif -ifdef CONFIG_COMPILER_STACK_CHECK_MODE_STRONG -COMMON_FLAGS += -fstack-protector-strong -endif -ifdef CONFIG_COMPILER_STACK_CHECK_MODE_ALL -COMMON_FLAGS += -fstack-protector-all -endif -endif - -# Optimization flags are set based on menuconfig choice -ifdef CONFIG_COMPILER_OPTIMIZATION_LEVEL_RELEASE -OPTIMIZATION_FLAGS = -Os -freorder-blocks -else -OPTIMIZATION_FLAGS = -Og +ifeq ($(SMING_RELEASE),1) +CPPFLAGS += -freorder-blocks endif -ifdef CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_DISABLE -CPPFLAGS += -DNDEBUG -endif - -# IDF uses some GNU extension from libc -CPPFLAGS += -D_GNU_SOURCE - -CPPFLAGS += -DCONFIG_NONE_OS - -# Enable generation of debugging symbols -# (we generate even in Release mode, as this has no impact on final binary size.) -DEBUG_FLAGS ?= -ggdb - -# List of flags to pass to C compiler -# If any flags are defined in application Makefile, add them at the end. -CFLAGS := $(strip \ - $(OPTIMIZATION_FLAGS) $(DEBUG_FLAGS) \ - $(COMMON_FLAGS) \ - $(COMMON_WARNING_FLAGS) -Wno-old-style-declaration \ - $(CPPFLAGS) \ - $(CFLAGS) \ - $(EXTRA_CFLAGS)) - -# List of flags to pass to C++ compiler -# If any flags are defined in application Makefile, add them at the end. -CXXFLAGS := $(strip \ - -std=gnu++11 \ - $(OPTIMIZATION_FLAGS) $(DEBUG_FLAGS) \ - $(COMMON_FLAGS) \ - $(COMMON_WARNING_FLAGS) \ - $(CPPFLAGS) \ - $(CXXFLAGS) \ - $(EXTRA_CXXFLAGS)) - -ifdef CONFIG_COMPILER_CXX_EXCEPTIONS -CXXFLAGS += -fexceptions -else -CXXFLAGS += -fno-exceptions -endif - -ifdef CONFIG_COMPILER_CXX_RTTI -CXXFLAGS += -frtti -else -CXXFLAGS += -fno-rtti -LDFLAGS += -fno-rtti -endif - -ARFLAGS := cru - # => Tools MEMANALYZER = $(PYTHON) $(ARCH_TOOLS)/memanalyzer.py $(OBJDUMP)$(TOOL_EXT) diff --git a/Sming/Arch/Esp8266/app.mk b/Sming/Arch/Esp8266/app.mk index 96ad91ec04..0ce2887fb5 100644 --- a/Sming/Arch/Esp8266/app.mk +++ b/Sming/Arch/Esp8266/app.mk @@ -45,25 +45,8 @@ endef $(TARGET_OUT_0): $(COMPONENTS_AR) $(LIBMAIN_DST) $(call LinkTarget,$(RBOOT_LD_0)) - - $(Q) $(MEMANALYZER) $@ > $(FW_MEMINFO_NEW) - - $(Q) if [ -f "$(FW_MEMINFO_NEW)" -a -f "$(FW_MEMINFO_OLD)" ]; then \ - $(AWK) -F "|" ' \ - FILENAME == "$(FW_MEMINFO_OLD)" { \ - arr[$$1]=$$5 \ - } \ - FILENAME == "$(FW_MEMINFO_NEW)" { \ - if (arr[$$1] != $$5) { \ - printf "%s%s%+d%s", substr($$0, 1, length($$0) - 1)," (",$$5 - arr[$$1],")\n" \ - } else { \ - print $$0 \ - } \ - }' $(FW_MEMINFO_OLD) $(FW_MEMINFO_NEW); \ - elif [ -f "$(FW_MEMINFO_NEW)" ]; then \ - cat $(FW_MEMINFO_NEW); \ - fi - + $(Q) $(MEMANALYZER) $@ > $(FW_MEMINFO) + $(Q) cat $(FW_MEMINFO) $(TARGET_OUT_1): $(COMPONENTS_AR) $(LIBMAIN_DST) $(call LinkTarget,$(RBOOT_LD_1)) diff --git a/Sming/Arch/Esp8266/build.mk b/Sming/Arch/Esp8266/build.mk index d3497ba66d..232658a06e 100644 --- a/Sming/Arch/Esp8266/build.mk +++ b/Sming/Arch/Esp8266/build.mk @@ -9,12 +9,7 @@ override ESP_VARIANT := endif CPPFLAGS += -DARCH_ESP8266 -CXXFLAGS += -fno-rtti -fno-exceptions -fno-threadsafe-statics - -# Required to access peripheral registers using structs -# e.g. `uint32_t value: 8` sitting at a byte or word boundary will be 'optimised' to -# an 8-bit fetch/store instruction which will not work; it must be a full 32-bit access. -CXXFLAGS += -fstrict-volatile-bitfields +CXXFLAGS += -fno-threadsafe-statics ## ESP_HOME sets the path where ESP tools and SDK are located. DEBUG_VARS += ESP_HOME diff --git a/Sming/Arch/Host/app.mk b/Sming/Arch/Host/app.mk index 0e5c61c07e..9a7bcac527 100644 --- a/Sming/Arch/Host/app.mk +++ b/Sming/Arch/Host/app.mk @@ -26,8 +26,8 @@ $(TARGET_OUT_0): $(COMPONENTS_AR) $(info $(notdir $(PROJECT_DIR)): Linking $@) $(Q) $(LD) $(addprefix -L,$(LIBDIRS)) $(LDFLAGS) -Wl,--start-group $(COMPONENTS_AR) $(addprefix -l,$(LIBS)) -Wl,--end-group -o $@ $(Q) $(call WriteFirmwareConfigFile,$@) - $(Q) $(MEMANALYZER) $@ > $(FW_MEMINFO_NEW) - $(Q) cat $(FW_MEMINFO_NEW) + $(Q) $(MEMANALYZER) $@ > $(FW_MEMINFO) + $(Q) cat $(FW_MEMINFO) ##@Tools .PHONY: valgrind diff --git a/Sming/Components/Storage/component.mk b/Sming/Components/Storage/component.mk index cc10fb3a10..5bd2caffbc 100644 --- a/Sming/Components/Storage/component.mk +++ b/Sming/Components/Storage/component.mk @@ -70,7 +70,6 @@ $(error Hardware configuration error) else ifneq ($(SMING_ARCH),$(SMING_ARCH_HW)) $(error Hardware configuration is for arch $(SMING_ARCH_HW), does not match SMING_ARCH ($(SMING_ARCH))) endif -COMPONENT_CXXFLAGS := -DPARTITION_TABLE_OFFSET=$(PARTITION_TABLE_OFFSET) COMPONENT_CPPFLAGS := -DPARTITION_TABLE_OFFSET=$(PARTITION_TABLE_OFFSET) # Function to evaluate expression against config diff --git a/Sming/Components/malloc_count/component.mk b/Sming/Components/malloc_count/component.mk index 4306eb9552..e0e6f5365c 100644 --- a/Sming/Components/malloc_count/component.mk +++ b/Sming/Components/malloc_count/component.mk @@ -26,6 +26,6 @@ MC_WRAP_FUNCS += \ strdup endif -EXTRA_LDFLAGS := $(call Wrap,$(MC_WRAP_FUNCS)) +EXTRA_LDFLAGS := $(call UndefWrap,$(MC_WRAP_FUNCS)) endif diff --git a/Sming/System/include/m_printf.h b/Sming/System/include/m_printf.h index bc3efc4991..ed36499842 100644 --- a/Sming/System/include/m_printf.h +++ b/Sming/System/include/m_printf.h @@ -56,10 +56,7 @@ size_t m_putc(char c); */ size_t m_nputs(const char* str, size_t length); -static inline size_t m_puts(const char* str) -{ - return m_nputs(str, strlen(str)); -} +size_t m_puts(const char* str); #ifdef __cplusplus } diff --git a/Sming/System/m_printf.cpp b/Sming/System/m_printf.cpp index 4f0c865955..46b29172ac 100644 --- a/Sming/System/m_printf.cpp +++ b/Sming/System/m_printf.cpp @@ -231,6 +231,10 @@ size_t m_nputs(const char* str, size_t length) return _puts_callback ? _puts_callback(str, length) : 0; } +size_t m_puts(const char* str) +{ + return m_nputs(str, strlen(str)); +} void m_printHex(const char* tag, const void* data, size_t len, int addr, size_t bytesPerLine) { diff --git a/Sming/build.mk b/Sming/build.mk index 4c06fdabd9..c38b928f9c 100644 --- a/Sming/build.mk +++ b/Sming/build.mk @@ -148,6 +148,11 @@ CPPFLAGS = \ -fdata-sections \ -ffunction-sections +# Required to access peripheral registers using structs +# e.g. `uint32_t value: 8` sitting at a byte or word boundary will be 'optimised' to +# an 8-bit fetch/store instruction which will not work; it must be a full 32-bit access. +CPPFLAGS += -fstrict-volatile-bitfields + CPPFLAGS += \ -Wall \ -Wpointer-arith \ @@ -183,7 +188,10 @@ else CPPFLAGS += -Os -g endif -CXXFLAGS += -felide-constructors +CXXFLAGS += \ + -felide-constructors \ + -fno-rtti \ + -fno-exceptions ifneq ($(STRICT),1) CXXFLAGS += -Wno-reorder @@ -196,8 +204,9 @@ DEBUG_VARS += GCC_VERSION GCC_VERSION := $(shell $(CC) -dumpversion) # Use c11 by default. Every architecture can override it -SMING_C_STD ?= c11 -CFLAGS += -std=$(SMING_C_STD) +DEBUG_VARS += SMING_C_STD +SMING_C_STD ?= c11 +CFLAGS += -std=$(SMING_C_STD) # Select C++17 if supported, defaulting to C++11 otherwise DEBUG_VARS += SMING_CXX_STD @@ -240,8 +249,27 @@ CLIB_PREFIX := clib- ToUpper = $(shell echo "$1" | tr 'a-z' 'A-Z') ToLower = $(shell echo "$1" | tr 'A-Z' 'a-z') -# Use with LDFLAGS to undefine and wrap a list of functions +# Use with LDFLAGS to define a symbol alias +# $1 -> List of alias=name pairs +define DefSym +$(foreach n,$1,-Wl,--defsym=$n) +endef + +# Use with LDFLAGS to undefine a list of symbols +# $1 -> List of symbols +define Undef +$(foreach n,$1,-u $n) +endef + +# Use with LDFLAGS to wrap a list of symbols +# $1 -> List of symbols define Wrap +$(foreach n,$1,-Wl,-wrap,$n) +endef + +# Use with LDFLAGS to undefine and wrap a list of symbols +# $1 -> List of symbols +define UndefWrap $(foreach n,$1,-u $n -Wl,-wrap,$n) endef diff --git a/Sming/building.rst b/Sming/building.rst index 89fe66115c..ab103ddb23 100644 --- a/Sming/building.rst +++ b/Sming/building.rst @@ -39,7 +39,7 @@ These are the main variables you need to be aware of: - **Esp8266** The default if not specified. :envvar:`ESP_HOME` must also be provided to locate SDK & tools. - - **Esp32** {todo} + - **Esp32** Supports ESP32 architecture. - **Host** builds a version of the library for native host debugging on Linux or Windows @@ -634,6 +634,9 @@ never overwritten. Will be visible **ONLY** to C++ code within the component. +.. envvar:: COMPONENT_CPPFLAGS + + Will be visible to both C and C++ code within the component. .. important:: diff --git a/Sming/component-wrapper.mk b/Sming/component-wrapper.mk index ae7d2c4cfd..06079ebab3 100644 --- a/Sming/component-wrapper.mk +++ b/Sming/component-wrapper.mk @@ -27,6 +27,7 @@ CUSTOM_BUILD := COMPONENT_TARGETS := EXTRA_OBJ := COMPONENT_CFLAGS := +COMPONENT_CPPFLAGS := COMPONENT_CXXFLAGS := COMPONENT_VARS := COMPONENT_RELINK_VARS := @@ -106,6 +107,7 @@ endif ifeq (,$(CUSTOM_BUILD)) CFLAGS += $(COMPONENT_CFLAGS) +CPPFLAGS += $(COMPONENT_CPPFLAGS) CXXFLAGS += $(COMPONENT_CXXFLAGS) # GCC 10 escapes ':' in path names which breaks GNU make for Windows so filter them diff --git a/Sming/project.mk b/Sming/project.mk index 37a393d241..384410acea 100644 --- a/Sming/project.mk +++ b/Sming/project.mk @@ -109,10 +109,8 @@ DEBUG_VARS += TARGET_OUT_0 CACHE_VARS += APP_NAME APP_NAME ?= app -# Firmware memory layout info files -FW_MEMINFO_NEW := $(FW_BASE)/fwMeminfo.new -FW_MEMINFO_OLD := $(FW_BASE)/fwMeminfo.old -FW_MEMINFO_SAVED := out/fwMeminfo +# Firmware memory layout info file +FW_MEMINFO := $(FW_BASE)/fwMeminfo.txt # List of Components we're going to parse, with duplicate libraries removed COMPONENTS := Sming