Skip to content

Commit

Permalink
espressif's ESP-IDF port (#4270)
Browse files Browse the repository at this point in the history
JerryScript-DCO-1.0-Signed-off-by: Philippe pgu-swir@users.noreply.github.com


Co-authored-by: Philippe <pgu-swir@users.noreply.github.com>
  • Loading branch information
pgu-swir and pgu-swir committed Oct 21, 2020
1 parent 8a0a86e commit f303017
Show file tree
Hide file tree
Showing 10 changed files with 513 additions and 0 deletions.
10 changes: 10 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,16 @@ if("${PLATFORM}" STREQUAL "DARWIN")
set(ENABLE_STRIP_MESSAGE " (FORCED BY PLATFORM)")
endif()

if("${PLATFORM}" STREQUAL "ESP-IDF")
set(JERRY_LIBM OFF)
set(ENABLE_LTO OFF)
set(ENABLE_STRIP OFF)

set(JERRY_LIBM_MESSAGE " (FORCED BY PLATFORM)")
set(ENABLE_LTO_MESSAGE " (FORCED BY PLATFORM)")
set(ENABLE_STRIP_MESSAGE " (FORCED BY PLATFORM)")
endif()

if(USING_TI)
set(ENABLE_STRIP OFF)

Expand Down
29 changes: 29 additions & 0 deletions cmake/toolchain-esp32.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Copyright JS Foundation and other contributors, http://js.foundation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set(CMAKE_SYSTEM_NAME esp-idf)
set(CMAKE_SYSTEM_PROCESSOR xtensa)

set(CMAKE_C_COMPILER xtensa-esp32-elf-gcc)
set(CMAKE_CXX_COMPILER xtensa-esp32-elf-g++)
set(CMAKE_ASM_COMPILER xtensa-esp32-elf-gcc)

# uncomment that if you want 4-bytes float (much faster on esp32)
#add_definitions(-DJERRY_NUMBER_TYPE_FLOAT64=0)

set(CMAKE_C_FLAGS "-mlongcalls -Wno-frame-address" CACHE STRING "C Compiler Base Flags")
set(CMAKE_CXX_FLAGS "-mlongcalls -Wno-frame-address" CACHE STRING "C++ Compiler Base Flags")

# Can be removed after gcc 5.2.0 support is removed (ref GCC_NOT_5_2_0)
set(CMAKE_EXE_LINKER_FLAGS "-nostdlib" CACHE STRING "Linker Base Flags")
16 changes: 16 additions & 0 deletions targets/esp-idf/CMakeLists.txt.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# assumes there is a component with this the following
# - set the JERRY_DIR wherever the jerryscript source code (the include files) is
# - a "lib" directory with the 2 libraries below

set(JERRY_DIR ${PROJECT_DIR}/../../jerryscript/)

idf_component_register(
SRC_DIRS ${JERRY_DIR}/targets/esp-idf
INCLUDE_DIRS ${JERRY_DIR}/jerry-core/include ${JERRY_DIR}/jerry-ext/include
)

add_prebuilt_library(libjerry-core lib/libjerry-core.a REQUIRES newlib PRIV_REQUIRES ${COMPONENT_NAME})
add_prebuilt_library(libjerry-ext lib/libjerry-ext.a PRIV_REQUIRES ${COMPONENT_NAME})

target_link_libraries(${COMPONENT_LIB} INTERFACE libjerry-core)
target_link_libraries(${COMPONENT_LIB} INTERFACE libjerry-ext)
28 changes: 28 additions & 0 deletions targets/esp-idf/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
This is a port for espressif's esp-idf (esp32). The LIBM, LTO and STRIP options should be disabled, so to build under the IDF toolchain, just run the following command

```
python tools\build.py --toolchain=cmake/toolchain-esp32.cmake --cmake-param "-GUnix Makefiles" --jerry-cmdline=OFF --jerry-port-default=OFF --jerry-libm=OFF --lto=OFF --strip=OFF
```

NB: the LIBM, STRIP and LTO might be disabled by platform as well. I strongly suggest limiting heap memorry with '--mem-heap=128' but that really depends on the SRAM avaiulable on your esp32.

Then copy the artefacts 'build/lib/\*.a' in an esp-idf component named 'jerryscript' (eg) and use a 'CMakeLists.txt' like this one

```
# assumes there is a component with this the following
# - set the JERRY_DIR wherever the jerryscript source code (the include files) is
# - a "lib" directory with the 2 libraries below
set(JERRY_DIR ${PROJECT_DIR}/../../jerryscript/)
idf_component_register(
SRC_DIRS ${JERRY_DIR}/targets/esp-idf
INCLUDE_DIRS ${JERRY_DIR}/jerry-core/include ${JERRY_DIR}/jerry-ext/include
)
add_prebuilt_library(libjerry-core lib/libjerry-core.a REQUIRES newlib PRIV_REQUIRES ${COMPONENT_NAME})
add_prebuilt_library(libjerry-ext lib/libjerry-ext.a PRIV_REQUIRES ${COMPONENT_NAME})
target_link_libraries(${COMPONENT_LIB} INTERFACE libjerry-core)
target_link_libraries(${COMPONENT_LIB} INTERFACE libjerry-ext)
```
67 changes: 67 additions & 0 deletions targets/esp-idf/date.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/* Copyright JS Foundation and other contributors, http://js.foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <stdlib.h>
#include <sys/time.h>

#include "jerryscript-port.h"

/**
* Default implementation of jerry_port_get_local_time_zone_adjustment. Uses the 'tm_gmtoff' field
* of 'struct tm' (a GNU extension) filled by 'localtime_r' if available on the
* system, does nothing otherwise.
*
* @return offset between UTC and local time at the given unix timestamp, if
* available. Otherwise, returns 0, assuming UTC time.
*/
double jerry_port_get_local_time_zone_adjustment (double unix_ms, /**< ms since unix epoch */
bool is_utc) /**< is the time above in UTC? */
{
struct tm tm;
char buf[8];
time_t now = (time_t) (unix_ms / 1000);

localtime_r (&now, &tm);

if (!is_utc)
{
strftime(buf, 8, "%z", &tm);
now -= -atof(buf) * 3600 * 1000 / 100;
localtime_r (&now, &tm);
}

strftime(buf, 8, "%z", &tm);

return -atof(buf) * 3600 * 1000 / 100;
} /* jerry_port_get_local_time_zone_adjustment */

/**
* Default implementation of jerry_port_get_current_time. Uses 'gettimeofday' if
* available on the system, does nothing otherwise.
*
* @return milliseconds since Unix epoch - if 'gettimeofday' is available and
* executed successfully,
* 0 - otherwise.
*/
double jerry_port_get_current_time (void)
{
struct timeval tv;

if (gettimeofday (&tv, NULL) == 0)
{
return ((double) tv.tv_sec) * 1000.0 + ((double) tv.tv_usec) / 1000.0;
}
return 0.0;
} /* jerry_port_get_current_time */
28 changes: 28 additions & 0 deletions targets/esp-idf/debugger.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* Copyright JS Foundation and other contributors, http://js.foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"

#include "jerryscript-port.h"

/**
* Default implementation of jerry_port_sleep. Uses 'nanosleep' or 'usleep' if
* available on the system, does nothing otherwise.
*/
void jerry_port_sleep (uint32_t sleep_time) /**< milliseconds to sleep */
{
vTaskDelay( sleep_time / portTICK_PERIOD_MS);
} /* jerry_port_sleep */
43 changes: 43 additions & 0 deletions targets/esp-idf/external-context.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/* Copyright JS Foundation and other contributors, http://js.foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "jerryscript-port.h"

/**
* Pointer to the current context.
* Note that it is a global variable, and is not a thread safe implementation.
* But I don't see how jerryscript can make that thread-safe, only the appication can
*/
static jerry_context_t *current_context_p = NULL;

/**
* Set the current_context_p as the passed pointer.
*/
void
jerry_port_default_set_current_context (jerry_context_t *context_p) /**< points to the created context */
{
current_context_p = context_p;
} /* jerry_port_default_set_current_context */

/**
* Get the current context.
*
* @return the pointer to the current context
*/
jerry_context_t *
jerry_port_get_current_context (void)
{
return current_context_p;
} /* jerry_port_get_current_context */
34 changes: 34 additions & 0 deletions targets/esp-idf/fatal.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/* Copyright JS Foundation and other contributors, http://js.foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_log.h"

#include "jerryscript-port.h"

static const char TAG[] = "JS";

/**
* Default implementation of jerry_port_fatal. Calls 'abort' if exit code is
* non-zero, 'exit' otherwise.
*/
void jerry_port_fatal (jerry_fatal_code_t code) /**< cause of error */
{
ESP_LOGE(TAG, "Fatal error %d", code);
vTaskSuspend(NULL);
abort();
} /* jerry_port_fatal */
Loading

0 comments on commit f303017

Please sign in to comment.