Skip to content
Merged
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
31 changes: 31 additions & 0 deletions build-scripts/esp-idf/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# wasm-micro-runtime as ESP-IDF component

You can build an ESP-IDF project with wasm-micro-runtime as a component:

- Make sure you have the ESP-IDF properly installed and setup
- In particular have the following paths set:
- `WAMR_PATH` to point to your wasm-micro-runtime repository
- `IDF_PATH` to point to your ESP-IDF
- `source $IDF_PATH/export.sh`
- Create a new project, e.g.: `idf.py create-project wamr-hello`
- In the newly created project folder edit the `CMakeList.txt`:

```
cmake_minimum_required(VERSION 3.5)

include($ENV{IDF_PATH}/tools/cmake/project.cmake)

set (COMPONENTS ${IDF_TARGET} main freertos esptool_py wamr)

list(APPEND EXTRA_COMPONENT_DIRS "$ENV{WAMR_PATH}/build-scripts/esp-idf")

project(wamr-hello)
```
- Develop your project in it's `main` component folder.

You can find an example [here](../../product-mini/platforms/esp-idf).

- Set target platform: `idf.py set-target esp32c3`
- Build: `idf.py build`
- Flash: `idf.py flash`
- Check the output: `idf.py monitor`
57 changes: 57 additions & 0 deletions build-scripts/esp-idf/wamr/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Copyright (C) 2021 Intel Corporation and others. All rights reserved.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

# Set WAMR's build options
if("${IDF_TARGET}" STREQUAL "esp32c3")
set(WAMR_BUILD_TARGET "RISCV32")
else()
set(WAMR_BUILD_TARGET "XTENSA")
endif()

set(WAMR_BUILD_PLATFORM "esp-idf")

if (NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif ()

if (NOT DEFINED WAMR_BUILD_INTERP)
set (WAMR_BUILD_INTERP 1)
endif ()

if (NOT DEFINED WAMR_BUILD_FAST_INTERP)
set (WAMR_BUILD_FAST_INTERP 1)
endif ()

if (NOT DEFINED WAMR_BUILD_AOT)
set (WAMR_BUILD_AOT 1)
endif ()

if (NOT DEFINED WAMR_BUILD_LIBC_BUILTIN)
set (WAMR_BUILD_LIBC_BUILTIN 1)
endif ()

if (NOT DEFINED WAMR_BUILD_APP_FRAMEWORK)
set (WAMR_BUILD_APP_FRAMEWORK 0)
endif ()

if (NOT CMAKE_BUILD_EARLY_EXPANSION)
if (WAMR_BUILD_TARGET STREQUAL "XTENSA")
idf_build_set_property(COMPILE_DEFINITIONS "-DBUILD_TARGET_XTENSA=1" APPEND)
endif ()
if (WAMR_BUILD_INTERP)
idf_build_set_property(COMPILE_DEFINITIONS "-DWASM_ENABLE_INTERP=1" APPEND)
endif ()
if (WAMR_BUILD_AOT)
idf_build_set_property(COMPILE_DEFINITIONS "-DWASM_ENABLE_AOT=1" APPEND)
endif ()

set(WAMR_ROOT_DIR ${CMAKE_CURRENT_LIST_DIR}/../../..)
include(${WAMR_ROOT_DIR}/build-scripts/runtime_lib.cmake)
endif()

idf_component_register(SRCS ${WAMR_RUNTIME_LIB_SOURCE} ${PLATFORM_SHARED_SOURCE}
INCLUDE_DIRS ${IWASM_DIR}/include ${UTILS_SHARED_DIR} ${PLATFORM_SHARED_DIR} ${PLATFORM_SHARED_DIR}/../include
REQUIRES pthread
)


2 changes: 1 addition & 1 deletion core/iwasm/aot/arch/aot_reloc_xtensa.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ put_imm16_to_addr(int16 imm16, int16 *addr)
if ((intptr_t)addr % 4 != 3) {
*(int32 *)bytes = *addr_aligned1;
*(int16 *)(bytes + ((intptr_t)addr % 4)) = imm16;
memcpy(addr_aligned1, bytes, 4);
*addr_aligned1 = *(int32 *)bytes;
}
else {
addr_aligned2 = (int32 *)(((intptr_t)addr + 3) & ~3);
Expand Down
10 changes: 8 additions & 2 deletions core/shared/platform/esp-idf/espidf_malloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ os_malloc(unsigned size)
uintptr_t *addr_field;

buf_origin = malloc(size + 8 + sizeof(uintptr_t));
if (!buf_origin) {
return NULL;
}
buf_fixed = buf_origin + sizeof(void *);
if ((uintptr_t)buf_fixed & (uintptr_t)0x7) {
buf_fixed = (void *)((uintptr_t)(buf_fixed + 8) & (~(uintptr_t)7));
Expand All @@ -34,12 +37,15 @@ os_realloc(void *ptr, unsigned size)
uintptr_t *addr_field;

if (!ptr) {
return NULL;
return os_malloc(size);
}

addr_field = ptr - sizeof(uintptr_t);
mem_origin = (void *)(*addr_field);
mem_new = realloc(mem_origin, size + 8 + sizeof(uintptr_t));
if (!mem_new) {
return NULL;
}

if (mem_origin != mem_new) {
mem_new_fixed = mem_new + sizeof(uintptr_t);
Expand All @@ -61,7 +67,7 @@ void
os_free(void *ptr)
{
void *mem_origin;
uintptr *addr_field;
uintptr_t *addr_field;

if (ptr) {
addr_field = ptr - sizeof(uintptr_t);
Expand Down
24 changes: 23 additions & 1 deletion core/shared/platform/esp-idf/espidf_memmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,34 @@
void *
os_mmap(void *hint, size_t size, int prot, int flags)
{
return os_malloc((int)size);
if (prot & MMAP_PROT_EXEC) {
// Memory allocation with MALLOC_CAP_EXEC will return 4-byte aligned
// Reserve extra 4 byte to fixup alignment and size for the pointer to
// the originally allocated address
void *buf_origin =
heap_caps_malloc(size + 4 + sizeof(uintptr_t), MALLOC_CAP_EXEC);
if (!buf_origin) {
return NULL;
}
void *buf_fixed = buf_origin + sizeof(void *);
if ((uintptr_t)buf_fixed & (uintptr_t)0x7) {
buf_fixed = (void *)((uintptr_t)(buf_fixed + 4) & (~(uintptr_t)7));
}

uintptr_t *addr_field = buf_fixed - sizeof(uintptr_t);
*addr_field = (uintptr_t)buf_origin;
return buf_fixed;
}
else {
return os_malloc(size);
}
}

void
os_munmap(void *addr, size_t size)
{
// We don't need special handling of the executable allocations
// here, free() of esp-idf handles it properly
return os_free(addr);
}

Expand Down
6 changes: 6 additions & 0 deletions core/shared/platform/esp-idf/espidf_platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,13 @@ os_time_get_boot_microsecond(void)
uint8 *
os_thread_get_stack_boundary(void)
{
#if defined(CONFIG_FREERTOS_USE_TRACE_FACILITY)
TaskStatus_t pxTaskStatus;
vTaskGetInfo(xTaskGetCurrentTaskHandle(), &pxTaskStatus, pdTRUE, eInvalid);
return pxTaskStatus.pxStackBase;
#else // !defined(CONFIG_FREERTOS_USE_TRACE_FACILITY)
return NULL;
#endif
}

int
Expand Down
2 changes: 2 additions & 0 deletions product-mini/platforms/esp-idf/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
sdkconfig
sdkconfig.old
75 changes: 5 additions & 70 deletions product-mini/platforms/esp-idf/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,77 +1,12 @@
# Copyright (C) 2019 Intel Corporation. All rights reserved.
# Copyright (C) 2019-21 Intel Corporation and others. All rights reserved.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

# from ESP-IDF 4.0 examples/build_system/cmake/idf_as_lib
cmake_minimum_required(VERSION 3.5)
project(wamr_on_esp32c3)

enable_language(ASM)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)

if (NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif ()
set (COMPONENTS ${IDF_TARGET} main freertos esptool_py wamr)
list(APPEND EXTRA_COMPONENT_DIRS "$ENV{WAMR_PATH}/build-scripts/esp-idf")

if("${IDF_TARGET}" STREQUAL "")
message(FATAL_ERROR "You need to set IDF_TARGET to your target string")
endif()

# Include for ESP-IDF build system functions
include($ENV{IDF_PATH}/tools/cmake/idf.cmake)
# Create idf::esp32c3 and idf::freertos static libraries
idf_build_process(${IDF_TARGET}
# try and trim the build; additional components
# will be included as needed based on dependency tree
#
# although esptool_py does not generate static library,
# processing the component is needed for flashing related
# targets and file generation
COMPONENTS ${IDF_TARGET} freertos esptool_py
SDKCONFIG ${CMAKE_BINARY_DIR}/sdkconfig
BUILD_DIR ${CMAKE_BINARY_DIR})

# Set WAMR's build options
if("${IDF_TARGET}" STREQUAL "esp32c3")
set(WAMR_BUILD_TARGET "RISCV32")
else()
set(WAMR_BUILD_TARGET "XTENSA")
add_compile_options(-DWAMR_BUILD_TARGET_XTENSA=1)
endif()

set(WAMR_BUILD_PLATFORM "esp-idf")

if (NOT DEFINED WAMR_BUILD_INTERP)
set (WAMR_BUILD_INTERP 0)
endif ()

if (NOT DEFINED WAMR_BUILD_FAST_INTERP)
set (WAMR_BUILD_FAST_INTERP 0)
endif ()

if (NOT DEFINED WAMR_BUILD_AOT)
set (WAMR_BUILD_AOT 1)
endif ()

if (NOT DEFINED WAMR_BUILD_LIBC_BUILTIN)
set (WAMR_BUILD_LIBC_BUILTIN 1)
endif ()

if (NOT DEFINED WAMR_BUILD_APP_FRAMEWORK)
set (WAMR_BUILD_APP_FRAMEWORK 0)
endif ()


# Set the compile time variable so that the right binary is selected
add_compile_options(-DWAMR_BUILD_INTERP=${WAMR_BUILD_INTERP})

set(WAMR_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../..)
include(${WAMR_ROOT_DIR}/build-scripts/runtime_lib.cmake)

# define WAMR as library and provide it the esp-idf srcs
add_library(vmlib ${WAMR_RUNTIME_LIB_SOURCE})
target_link_libraries(vmlib PUBLIC idf::pthread idf::${IDF_TARGET} idf::freertos)

# Define the final executable
set(elf_file ${CMAKE_PROJECT_NAME}.elf)
add_executable(${elf_file} main.c test_wasm.h)
target_link_libraries(${elf_file} idf::${IDF_TARGET} idf::freertos idf::spi_flash vmlib)
idf_build_executable(${elf_file})
project(wamr-simple)
4 changes: 0 additions & 4 deletions product-mini/platforms/esp-idf/build.sh

This file was deleted.

33 changes: 33 additions & 0 deletions product-mini/platforms/esp-idf/build_and_run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/bin/bash -e

# Copyright (C) 2019-21 Intel Corporation and others. All rights reserved.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

ESP32_TARGET="esp32"
ESP32C3_TARGET="esp32c3"

usage ()
{
echo "USAGE:"
echo "$0 $ESP32_TARGET|$ESP32C3_TARGET"
echo "Example:"
echo " $0 $ESP32_TARGET"
echo " $0 $ESP32C3_TARGET"
exit 1
}

if [ $# != 1 ] ; then
usage
fi

TARGET=$1

if [[ -z "${WAMR_PATH}" ]]; then
export WAMR_PATH=$PWD/../../..
fi

rm -rf build
idf.py set-target $TARGET
idf.py build
idf.py flash

Loading