diff --git a/targets/nuttx-stm32f4/Makefile.nuttx b/targets/nuttx-stm32f4/Makefile.nuttx new file mode 100644 index 0000000000..5780667410 --- /dev/null +++ b/targets/nuttx-stm32f4/Makefile.nuttx @@ -0,0 +1,69 @@ +# Copyright 2016 Samsung Electronics Co., Ltd. +# +# 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. + +# use TAB-8 + +CURDIR = `pwd` +NUTTX ?= $(CURDIR)/../nuttx/nuttx +TYPE ?= release +JERRYHEAP ?= 64 +STFLASH ?= ../../stlink/st-flash + +NUTTXINC = $(NUTTX)/include +NUTTXLIB = $(NUTTX)/lib +INTERM = build/obj-nuttx-stm32f4 +OUTPUT = build/bin/$(TYPE).nuttx-stm32f4 +COPYTARGET = $(NUTTXLIB) + +EXT_CFLAGS := -mlittle-endian -mthumb -mcpu=cortex-m4 -march=armv7e-m +EXT_CFLAGS += -mfpu=fpv4-sp-d16 -mfloat-abi=hard -Wno-conversion + + +.PHONY: nutx flash clean + + +all: + mkdir -p $(INTERM) + mkdir -p $(OUTPUT) + cmake -B$(INTERM) -H./ \ + -DENABLE_LTO=OFF \ + -DENABLE_VALGRIND=OFF \ + -DCMAKE_TOOLCHAIN_FILE=build/configs/toolchain_external.cmake \ + -DEXTERNAL_CMAKE_SYSTEM_PROCESSOR=armv7l-hf \ + -DEXTERNAL_CMAKE_C_COMPILER=arm-none-eabi-gcc \ + -DEXTERNAL_CMAKE_CXX_COMPILER=arm-none-eabi-g++ \ + -DEXTERNAL_BUILD_ENTRY_FILE=./targets/nuttx-stm32f4/main-nuttx.cpp \ + -DEXTERNAL_COMPILE_FLAGS="$(EXT_CFLAGS)" \ + -DEXTERNAL_LIBC_INTERFACE=$(NUTTXINC) \ + -DEXTERNAL_CMAKE_SYSTEM_PROCESSOR=arm \ + -DEXTERNAL_MEM_HEAP_SIZE_KB=$(JERRYHEAP) + make -C $(INTERM) $(TYPE).external + cp `cat $(INTERM)/$(TYPE).external/list` $(OUTPUT)/. + cp $(OUTPUT)/lib$(TYPE).jerry-core.a $(COPYTARGET)/libjerrycore.a + cp $(OUTPUT)/lib$(TYPE).jerry-fdlibm.third_party.lib.a \ + $(COPYTARGET)/libfdlibm.a + cp $(INTERM)/lib$(TYPE).external-entry.a $(OUTPUT)/. + cp $(OUTPUT)/lib$(TYPE).external-entry.a $(COPYTARGET)/libjerryentry.a + + +nuttx: + cd $(NUTTX); make + + +flash: + cd $(NUTTX); $(STFLASH) write nuttx.bin 0x8000000 + +clean: + rm -rf $(INTERM) + rm -rf $(OUTPUT) diff --git a/targets/nuttx-stm32f4/README.md b/targets/nuttx-stm32f4/README.md new file mode 100644 index 0000000000..4ab782eabf --- /dev/null +++ b/targets/nuttx-stm32f4/README.md @@ -0,0 +1,178 @@ +### About + +This folder contains files to run JerryScript on NuttX with +[STM32F4-Discovery with BB](http://www.st.com/web/en/catalog/tools/FM116/SC959/SS1532/LN1199/PF255417) + + +### How to build + +#### 1. Preface + +1, Directory structure + +Assume `harmony` as the root folder to the projects to build. +The folder tree related would look like this. + +``` +harmony + + jerryscript + | + targets + | + nuttx-stm32f4 + + nuttx + | + nuttx + | + lib + + st-link +``` + + +2, Target board + +Assume [STM32F4-Discovery with BB](http://www.st.com/web/en/catalog/tools/FM116/SC959/SS1532/LN1199/PF255417) +as the target board. + + +3, Micro SD-Card memory for Script source files + + +#### 2. Prepare NuttX + +Follow [this](https://bitbucket.org/seanshpark/nuttx/wiki/Home) page to get +NuttX source and do the first build. When it stops with and error, +change default project from `IoT.js` to `JerryScript` as follows; + +2-1) run menuconfig +``` +# assume you are in nuttx folder where .config exist +make menuconfig +``` +2-2) Select `Application Configuration` --> `Interpreters` + +2-3) Check `[*] JerryScript interpreter` (Move cursor to the line and press `Space`) + +2-4) `< Exit >` once on the bottom of the sceen (Press `Right arrow` and `Enter`) + +2-5) Select `System Libraries and NSH Add-Ons` + +2-6) Un-Check `[ ] iotjs program` (Move cursor to the line and press `Space`) + +2-7) `< Exit >` till `menuconfig` ends. Save new configugation when asked. + +2-7) `make` again +``` +make +``` + +It'll show the last error but it's ok. Nows the time to build JerryScript. + + +#### 3. Build JerryScript for NuttX + +``` +# assume you are in harmony folder +cd jerryscript +make -f ./targets/nuttx-stm32f4/Makefile.nuttx +``` + +If you have NuttX at another path than described above, you can give the +absolute path with `NUTTX` variable , for example, +``` +NUTTX=/home/user/work/nuttx make -f ./targets/nuttx-stm32f4/Makefile.nuttx +``` + +Make will copy three library files to `nuttx/nuttx/lib` folder +``` +libjerryentry.a +libjerrycore.a +libfdlibm.a +``` + +In NuttX, if you run `make clean`, above library files are also removed so you +may have to build JerryScript again. + +#### 4. Continue build NuttX + +``` +# asssume you are in harmony folder +cd nuttx/nuttx +make +``` + + +#### 5. Flashing + +Connect Mini-USB for power supply and connect Micro-USB for `NSH` console. + +Please refer [this](https://github.com/Samsung/iotjs/wiki/Build-for-NuttX#prepare-flashing-to-target-board) +link to prepare `stlink` utility. + + +To flash with `Makefile.nuttx`, +``` +# assume you are in jerryscript folder +make -f ./targets/nuttx-stm32f4/Makefile.nuttx flash +``` + +#### 6. Cleaning + +To clean the build result, +``` +make -f ./targets/nuttx-stm32f4/Makefile.nuttx clean +``` + + +### Running JerryScript + +Prepare a micro SD-card and prepare `hello.js` like this in the root directory of SD-card. + +``` +print("Hello JerryScript!"); +``` + +Power Off(unplug both USB cables), plug the memory card to BB, and power on again. + +You can use `minicom` for terminal program, or any other you may like, but match +baud rate to `115200`. + +``` +minicom --device=/dev/ttyACM0 --baud=115200 +``` + + +You may have to press `RESET` on the board and press `Enter` keys on the console +several times to make `nsh` prompt to appear. + +If the prompt shows like this, +``` +NuttShell (NSH) + nsh> + nsh> + nsh> +``` +please set `Add Carriage Ret` option by `CTRL-A` > `Z` > `U` at the console, +if you're using `minicom`. + + +Run `jerryscript` with `hello.js` + +``` +NuttShell (NSH) +nsh> +nsh> +nsh> jerryscript /mnt/sdcard/hello.js +PARAM 1 : [/mnt/sdcard/hello.js] +Hello JerryScript! +``` + +Please give absolute path of the script file or may get an error like this. +``` +nsh> cd /mnt/sdcard +nsh> jerryscript ./hello.js +PARAM 1 : [./hello.js] +Failed to fopen [./hello.js] +JERRY_STANDALONE_EXIT_CODE_FAIL +nsh> +nsh> +nsh> jerryscript /mnt/sdcard/hello.js +PARAM 1 : [/mnt/sdcard/hello.js] +Hello JerryScript! +``` diff --git a/targets/nuttx-stm32f4/main-nuttx.cpp b/targets/nuttx-stm32f4/main-nuttx.cpp new file mode 100644 index 0000000000..19e191b604 --- /dev/null +++ b/targets/nuttx-stm32f4/main-nuttx.cpp @@ -0,0 +1,289 @@ +/* Copyright 2016 Samsung Electronics Co., Ltd. + * + * 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 +#include +#include + +#include "jerry.h" +#include "jrt/jrt.h" + +/** + * The module interface routine + */ +extern "C" int jerryscript_entry (int argc, char *argv[]); + +/** + * Maximum command line arguments number + */ +#define JERRY_MAX_COMMAND_LINE_ARGS (16) + +/** + * Standalone Jerry exit codes + */ +#define JERRY_STANDALONE_EXIT_CODE_OK (0) +#define JERRY_STANDALONE_EXIT_CODE_FAIL (1) + +static char* read_sources (const char *script_file_names[], + int files_count, + size_t *out_source_size_p) +{ + int i; + char* source_buffer = NULL; + char *source_buffer_tail = NULL; + size_t total_length = 0; + FILE *file = NULL; + + for (i = 0; i < files_count; i++) + { + const char *script_file_name = script_file_names[i]; + + file = fopen (script_file_name, "r"); + if (file == NULL) + { + JERRY_ERROR_MSG ("Failed to fopen [%s]\n", script_file_name); + return NULL; + } + + int fseek_status = fseek (file, 0, SEEK_END); + if (fseek_status != 0) + { + JERRY_ERROR_MSG ("Failed to fseek fseek_status(%d)\n", fseek_status); + fclose (file); + return NULL; + } + + long script_len = ftell (file); + if (script_len < 0) + { + JERRY_ERROR_MSG ("Failed to ftell script_len(%ld)\n", script_len); + fclose (file); + break; + } + + total_length += (size_t)script_len; + + fclose (file); + file = NULL; + } + + if (total_length <= 0) + { + JERRY_ERROR_MSG ("Theres noting to read\n"); + return NULL; + } + + source_buffer = (char*)malloc(total_length); + if (source_buffer == NULL) + { + JERRY_ERROR_MSG ("Out of memory error\n"); + return NULL; + } + memset(source_buffer, 0, sizeof(char)*total_length); + source_buffer_tail = source_buffer; + + for (i = 0; i < files_count; i++) + { + const char *script_file_name = script_file_names[i]; + file = fopen (script_file_name, "r"); + + if (file == NULL) + { + JERRY_ERROR_MSG ("Failed to fopen [%s]\n", script_file_name); + break; + } + + int fseek_status = fseek (file, 0, SEEK_END); + if (fseek_status != 0) + { + JERRY_ERROR_MSG ("Failed to fseek fseek_status(%d)\n", fseek_status); + break; + } + + long script_len = ftell (file); + if (script_len < 0) + { + JERRY_ERROR_MSG ("Failed to ftell script_len(%ld)\n", script_len); + break; + } + + rewind (file); + + const size_t current_source_size = (size_t)script_len; + size_t bytes_read = fread (source_buffer_tail, 1, current_source_size, file); + if (bytes_read < current_source_size) + { + JERRY_ERROR_MSG ("Failed to fread bytes_read(%d)\n", bytes_read); + break; + } + + fclose (file); + file = NULL; + + source_buffer_tail += current_source_size; + } + + if (file != NULL) + { + fclose (file); + } + + if (i < files_count) + { + JERRY_ERROR_MSG ("Failed to read script N%d\n", i + 1); + free(source_buffer); + return NULL; + } + + *out_source_size_p = (size_t)total_length; + + return source_buffer; +} + +int jerryscript_entry (int argc, char *argv[]) +{ + if (argc >= JERRY_MAX_COMMAND_LINE_ARGS) + { + JERRY_ERROR_MSG ("Too many command line arguments. Current maximum is %d\n", + JERRY_MAX_COMMAND_LINE_ARGS); + + return JERRY_STANDALONE_EXIT_CODE_FAIL; + } + + const char *file_names[JERRY_MAX_COMMAND_LINE_ARGS]; + int i; + int files_counter = 0; + + for (i = 1; i < argc; i++) + { + printf ("PARAM %d : [%s]\n", i, argv[i]); + } + + jerry_flag_t flags = JERRY_FLAG_EMPTY; + + for (i = 1; i < argc; i++) + { + if (!strcmp ("-v", argv[i])) + { + printf ("Build date: \t%s\n", jerry_build_date); + printf ("Commit hash:\t%s\n", jerry_commit_hash); + printf ("Branch name:\t%s\n", jerry_branch_name); + } + else if (!strcmp ("--mem-stats", argv[i])) + { + flags |= JERRY_FLAG_MEM_STATS; + } + else if (!strcmp ("--mem-stats-per-opcode", argv[i])) + { + flags |= JERRY_FLAG_MEM_STATS_PER_OPCODE; + } + else if (!strcmp ("--mem-stats-separate", argv[i])) + { + flags |= JERRY_FLAG_MEM_STATS_SEPARATE; + } + else if (!strcmp ("--parse-only", argv[i])) + { + flags |= JERRY_FLAG_PARSE_ONLY; + } + else if (!strcmp ("--show-opcodes", argv[i])) + { + flags |= JERRY_FLAG_SHOW_OPCODES; + } + else if (!strcmp ("--abort-on-fail", argv[i])) + { + flags |= JERRY_FLAG_ABORT_ON_FAIL; + } + else if (!strcmp ("--log-level", argv[i])) + { + flags |= JERRY_FLAG_ENABLE_LOG; + if (++i < argc && strlen (argv[i]) == 1 && argv[i][0] >='0' && argv[i][0] <= '3') + { +#ifdef JERRY_ENABLE_LOG + jerry_debug_level = argv[i][0] - '0'; +#endif /* JERRY_ENABLE_LOG */ + } + else + { + JERRY_ERROR_MSG ("Error: wrong format or invalid argument\n"); + return JERRY_STANDALONE_EXIT_CODE_FAIL; + } + } + else + { + file_names[files_counter++] = argv[i]; + } + } + + if (files_counter == 0) + { + printf ("Jerry: file count 0\n"); + return JERRY_STANDALONE_EXIT_CODE_OK; + } + + size_t source_size; + char *source_p = read_sources (file_names, files_counter, &source_size); + + if (source_p == NULL) + { + JERRY_ERROR_MSG ("JERRY_STANDALONE_EXIT_CODE_FAIL\n"); + return JERRY_STANDALONE_EXIT_CODE_FAIL; + } + + jerry_completion_code_t ret_code; + + ret_code = jerry_run_simple ((jerry_api_char_t *) source_p, source_size, flags); + + free(source_p); + + if (ret_code != JERRY_COMPLETION_CODE_OK) + { + return JERRY_STANDALONE_EXIT_CODE_FAIL; + } + return JERRY_STANDALONE_EXIT_CODE_OK; +} + +/** + * Provide log message to filestream implementation for the engine. + */ +int jerry_port_logmsg (FILE* stream, const char* format, ...) +{ + va_list args; + int count; + va_start (args, format); + count = vfprintf (stream, format, args); + va_end (args); + return count; +} + +/** + * Provide error message to console implementation for the engine. + */ +int jerry_port_errormsg (const char* format, ...) +{ + va_list args; + int count; + va_start (args, format); + count = vfprintf (stderr, format, args); + va_end (args); + return count; +} + +/** + * Provide output character to console implementation for the engine. + */ +int jerry_port_putchar (int c) +{ + return putchar(c); +}