From 236c96ec42048470382e854cac3e7514ca0b97c6 Mon Sep 17 00:00:00 2001 From: Imre Kiss Date: Mon, 4 Apr 2016 09:41:31 +0200 Subject: [PATCH 1/3] New target for Discovery STM32F4 board JerryScript-DCO-1.0-Signed-off-by: Imre Kiss kissi@inf.u-szeged.hu --- targets/mbedstm32f4/.yotta.json | 6 + targets/mbedstm32f4/Makefile.mbedstm32f4 | 67 +++++++ targets/mbedstm32f4/js/blink.js | 9 + targets/mbedstm32f4/js/main.js | 5 + targets/mbedstm32f4/module.json | 12 ++ targets/mbedstm32f4/readme.md | 170 ++++++++++++++++++ targets/mbedstm32f4/source/jerry_extapi.cpp | 130 ++++++++++++++ targets/mbedstm32f4/source/jerry_extapi.h | 38 ++++ targets/mbedstm32f4/source/jerry_port.c | 55 ++++++ targets/mbedstm32f4/source/jerry_run.cpp | 125 +++++++++++++ targets/mbedstm32f4/source/jerry_run.h | 24 +++ targets/mbedstm32f4/source/main.cpp | 75 ++++++++ targets/mbedstm32f4/source/makejerry.cmake | 30 ++++ .../mbedstm32f4/source/native_mbedstm32f4.cpp | 25 +++ .../mbedstm32f4/source/native_mbedstm32f4.h | 21 +++ 15 files changed, 792 insertions(+) create mode 100644 targets/mbedstm32f4/.yotta.json create mode 100644 targets/mbedstm32f4/Makefile.mbedstm32f4 create mode 100644 targets/mbedstm32f4/js/blink.js create mode 100644 targets/mbedstm32f4/js/main.js create mode 100644 targets/mbedstm32f4/module.json create mode 100644 targets/mbedstm32f4/readme.md create mode 100644 targets/mbedstm32f4/source/jerry_extapi.cpp create mode 100644 targets/mbedstm32f4/source/jerry_extapi.h create mode 100644 targets/mbedstm32f4/source/jerry_port.c create mode 100644 targets/mbedstm32f4/source/jerry_run.cpp create mode 100644 targets/mbedstm32f4/source/jerry_run.h create mode 100644 targets/mbedstm32f4/source/main.cpp create mode 100644 targets/mbedstm32f4/source/makejerry.cmake create mode 100644 targets/mbedstm32f4/source/native_mbedstm32f4.cpp create mode 100644 targets/mbedstm32f4/source/native_mbedstm32f4.h diff --git a/targets/mbedstm32f4/.yotta.json b/targets/mbedstm32f4/.yotta.json new file mode 100644 index 0000000000..eaed7515c2 --- /dev/null +++ b/targets/mbedstm32f4/.yotta.json @@ -0,0 +1,6 @@ +{ + "build": { + "target": "stm32f4-disco-gcc,*", + "targetSetExplicitly": true + } +} diff --git a/targets/mbedstm32f4/Makefile.mbedstm32f4 b/targets/mbedstm32f4/Makefile.mbedstm32f4 new file mode 100644 index 0000000000..abe8ce2679 --- /dev/null +++ b/targets/mbedstm32f4/Makefile.mbedstm32f4 @@ -0,0 +1,67 @@ +# Copyright 2015 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` +TYPE = release +INTERM = build/obj-mbed-stm32f4 +OUTPUT = build/bin/$(TYPE).mbedstm32f4 +COPYTARGET = targets/mbedstm32f4/libjerry +JERRYHEAP ?= 16 + +EXT_CFLAGS := -D__TARGET_MBED_STM32F4 +EXT_CFLAGS += -mlittle-endian -mthumb -mcpu=cortex-m4 +EXT_CFLAGS += -Wno-error=format= + +.PHONY: jerry js2c yotta flash clean + +all: jerry js2c yotta + +jerry: + mkdir -p $(INTERM) + mkdir -p $(OUTPUT) + mkdir -p $(COPYTARGET) + cmake -B$(INTERM) -H./ \ + -DENABLE_LTO=OFF \ + -DENABLE_VALGRIND=OFF \ + -DCMAKE_TOOLCHAIN_FILE=build/configs/toolchain_external.cmake \ + -DUSE_COMPILER_DEFAULT_LIBC=YES \ + -DEXTERNAL_CMAKE_SYSTEM_PROCESSOR=arm7-m \ + -DEXTERNAL_CMAKE_C_COMPILER=arm-none-eabi-gcc \ + -DEXTERNAL_CMAKE_C_COMPILER_ID=GNU \ + -DEXTERNAL_COMPILE_FLAGS="$(EXT_CFLAGS)" \ + -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 + +js2c: + cd targets/mbedstm32f4; ../tools/js2c.py; + +yotta: + cd targets/mbedstm32f4; \ + yotta target stm32f4-disco-gcc; \ + yotta build + +flash: + st-flash write targets/mbedstm32f4/build/stm32f4-disco-gcc/source/jerry.bin 0x08000000 + @echo "Jerry on the board!" + +clean: + rm -rf $(INTERM) + rm -rf $(OUTPUT) + rm -rf targets/mbedstm32f4/build diff --git a/targets/mbedstm32f4/js/blink.js b/targets/mbedstm32f4/js/blink.js new file mode 100644 index 0000000000..0e51261393 --- /dev/null +++ b/targets/mbedstm32f4/js/blink.js @@ -0,0 +1,9 @@ +var check = 1; + +function blink() { + var blk = (check > 8) ? 1 : 0; + led(1, blk); + check = (check >= 10) ? 1 : check+1; +} + +print("blink js OK"); diff --git a/targets/mbedstm32f4/js/main.js b/targets/mbedstm32f4/js/main.js new file mode 100644 index 0000000000..7063198045 --- /dev/null +++ b/targets/mbedstm32f4/js/main.js @@ -0,0 +1,5 @@ +function sysloop(ticknow) { + blink(); +} + +print("main js OK"); diff --git a/targets/mbedstm32f4/module.json b/targets/mbedstm32f4/module.json new file mode 100644 index 0000000000..ab872ef1da --- /dev/null +++ b/targets/mbedstm32f4/module.json @@ -0,0 +1,12 @@ +{ + "name": "jerry", + "version": "0.0.1", + "bin": "./source", + "private": true, + "description": "JerryScript in mbed OS - STM32F4", + "author": "Imre Kiss", + "license": "Apache-2.0", + "dependencies": { + "mbed-drivers": "^1.5.0" + } +} diff --git a/targets/mbedstm32f4/readme.md b/targets/mbedstm32f4/readme.md new file mode 100644 index 0000000000..199fbc391c --- /dev/null +++ b/targets/mbedstm32f4/readme.md @@ -0,0 +1,170 @@ +This folder contains files to run JerryScript in mbed / DISCO-F407VG board (STM32F4). + +#### yotta + +You need to install yotta before proceeding. +Please visit http://yottadocs.mbed.com/#installing-on-linux + +#### Yotta module dependecies + +This board not supported yet. But there is a way to run mbed OS v3.0 and JerryScript on this board. + +- Select the yotta target +``` +yotta target stm32f4-disco-gcc +``` +This is not an offical target for this board (yet). But you can able to build it. + +- Install mbed Drivers +``` +yotta install mbed-drivers +``` + +- Modify the yotta module dependecies +There are two missing target dependecies in the module tree. +First module: mbed-hal-st-stm32f407vg +Second module: cmsis-core-stm32f407xg +You have to add these modules manually. +``` +cd yotta_modules/mbed-hal-st-stm32f4 +open module.json in an editor +add these lines to the targetDependencies: + +"stm32f407vg": { + "mbed-hal-st-stm32f407vg": "^0.0.1" +} + +cd ../cmsis-core-st +open module.json in an editor +add these lines to tha targetDependencies: + +"stm32f407xg": { + "cmsis-core-stm32f407xg": "^0.0.1" +} +``` + +- Update yotta +After the changes, you have to update or build yotta +``` +yotta build +``` + +Note: if you run into a module version error after modified the modules tree, you have to check the `mbed-hal-st-stm32f407vg module.json` file’s dependencies block. The correct uvisor-lib version specification is “>=1.0.2”. Should looks like this: + +``` +"dependencies": { + "cmsis-core": "^1.0.0", + "uvisor-lib": ">=1.0.2" + } +``` + +- Check the yotta "module tree" +``` +yotta ls +``` +If everything is good, you will get this output: +``` +jerry 0.0.0 +┗━ mbed-drivers 0.11.8 + ┣━ mbed-hal 1.2.1 yotta_modules/mbed-hal + ┃ ┗━ mbed-hal-st 1.0.0 yotta_modules/mbed-hal-st + ┃ ┗━ mbed-hal-st-stm32f4 1.1.2 yotta_modules/mbed-hal-st-stm32f4 + ┃ ┣━ uvisor-lib 1.0.12 yotta_modules/uvisor-lib + ┃ ┣━ mbed-hal-st-stm32cubef4 1.0.2 yotta_modules/mbed-hal-st-stm32cubef4 + ┃ ┗━ mbed-hal-st-stm32f407vg 0.0.1 yotta_modules/mbed-hal-st-stm32f407vg + ┣━ cmsis-core 1.1.1 yotta_modules/cmsis-core + ┃ ┗━ cmsis-core-st 1.0.0 yotta_modules/cmsis-core-st + ┃ ┣━ cmsis-core-stm32f4 1.0.5 yotta_modules/cmsis-core-stm32f4 + ┃ ┗━ cmsis-core-stm32f407xg 0.0.1 yotta_modules/cmsis-core-stm32f407xg + ┣━ ualloc 1.0.3 yotta_modules/ualloc + ┃ ┗━ dlmalloc 1.0.0 yotta_modules/dlmalloc + ┣━ minar 1.0.4 yotta_modules/minar + ┃ ┗━ minar-platform 1.0.0 yotta_modules/minar-platform + ┃ ┗━ minar-platform-mbed 1.0.0 yotta_modules/minar-platform-mbed + ┣━ core-util 1.1.5 yotta_modules/core-util + ┗━ compiler-polyfill 1.2.1 yotta_modules/compiler-polyfill +``` + +##### Cross-compiler for DISCO-F4 + +If you don't have any GCC compiler installed, please visit [this] +(https://launchpad.net/gcc-arm-embedded/+download) page to download GCC 5.2.1 +which was tested for building JerryScript for STM32F4. + +#### How to build JerryScript +It assumes the current folder is the JerryScript root folder. + +``` +make -f targets/mbedstm32f4/Makefile.mbedstm32f4 clean +make -f targets/mbedstm32f4/Makefile.mbedstm32f4 jerry +``` + +#### JerryScript output files + +Two files will be generated at `targets/mbedstm32f4/libjerry` +* libjerrycore.a +* libfdlibm.a + +#### Building mbed binary + +``` +make -f targets/mbedstm32f4/Makefile.mbedstm32f4 js2c yotta +``` + +#### The binary file + +The file will be generated at targets/mbedstm32f4/build/stm32f4-disco-gcc/source/jerry.bin + +#### Flashing to STM32F4 + +This borad does not support the drag&drop and Virtual Comm Port features. You will have to use an external tool (for example the STM32 STLink utility) to program your code .bin file. + +If you don't have STlink installed, please visit [this] +(https://github.com/texane/stlink) page to install STLink-v2. + +``` +make -f targets/mbedstm32f4/Makefile.mbedstm32f4 flash +``` +OR +``` +cd targets/mbedstm32f4/build/stm32f4-disco-gcc/source +st-flash write jerry.bin 0x08000000 +``` +It assumes the st-flash there is in your /usr/bin folder. + +When LED near the USB port flashing stops, press RESET button on the board to execute +JerryScript led flashing sample program in js folder. + +#### How blink sample program works + +All `.js` files in `js` folder are executed, with `main.js` in first order. +`sysloop()` function in main.js is called periodically in every 100msec by +below code in `main.cpp` `jerry_loop()` (after the `jerrry_init()` finished), which calls `js_loop()`. + +``` + minar::Scheduler::postCallback(jerry_loop).period(minar::milliseconds(100)) +``` + +`sysloop()` then calls `blink()` in `blink.js` which blinks the `LED` in +every second. + +#### printf + +There is no the possibility to use the printf directly in your code. +If you want to use it, you have to use a TTL to USB adapter and a serial communcation program, +like [this program](http://www.cyberciti.biz/tips/connect-soekris-single-board-computer-using-minicom.html) and [this adapter](http://www.cpmspectrepi.webspace.virginmedia.com/raspberry_pi/MoinMoinExport/USBtoTtlSerialAdapters.html). + +Connect the adapter to the board with the USBTX, USBRX pin (PA10, PA9 in this case), then the board will appear in your host /dev folder. + +``` +/dev/ttyUSB0 +``` + +After you connected the board to the host over the adapter and configurated your serial communicate program, +you can able use the printf the following way: +(note: the default baud rate is 9600) + +``` +Serial pc(USBTX, USBRX); //tx, rx +pc.printf("%s\n\r", str); +``` diff --git a/targets/mbedstm32f4/source/jerry_extapi.cpp b/targets/mbedstm32f4/source/jerry_extapi.cpp new file mode 100644 index 0000000000..caa5dbb4b8 --- /dev/null +++ b/targets/mbedstm32f4/source/jerry_extapi.cpp @@ -0,0 +1,130 @@ +/* Copyright 2014-2015 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 "jerry-core/jerry.h" + +#include +#include + +#include "jerry_extapi.h" +#include "native_mbedstm32f4.h" + +#ifndef MIN +#define MIN(A,B) ((A)<(B)?(A):(B)) +#endif + +//----------------------------------------------------------------------------- + +#define __UNSED__ __attribute__((unused)) + +#define DECLARE_HANDLER(NAME) \ +static bool \ +NAME ## _handler (const jerry_api_object_t * function_obj_p __UNSED__, \ + const jerry_api_value_t * this_p __UNSED__, \ + jerry_api_value_t * ret_val_p __UNSED__, \ + const jerry_api_value_t args_p[], \ + const jerry_api_length_t args_cnt) + +#define REGISTER_HANDLER(NAME) \ + register_native_function ( # NAME, NAME ## _handler) + +//----------------------------------------------------------------------------- + +DECLARE_HANDLER(assert) +{ + if (args_cnt == 1 + && args_p[0].type == JERRY_API_DATA_TYPE_BOOLEAN + && args_p[0].u.v_bool == true) + { + printf (">> Jerry assert true\r\n"); + return true; + } + printf ("ERROR: Script assertion failed\n"); + exit (JERRY_STANDALONE_EXIT_CODE_FAIL); + return false; +} + +DECLARE_HANDLER(led) +{ + if (args_cnt < 2) + { + return false; + } + + int port, value; + port = (int)JS_VALUE_TO_NUMBER (&args_p[0]); + value = (int)JS_VALUE_TO_NUMBER (&args_p[1]); + + ret_val_p->type = JERRY_API_DATA_TYPE_BOOLEAN; + if (port >=0 && port <= 3) + { + native_led(port, value); + ret_val_p->u.v_bool = true; + } + else + { + ret_val_p->u.v_bool = false; + } + return true; +} + +//----------------------------------------------------------------------------- + +static bool +register_native_function (const char* name, + jerry_external_handler_t handler) +{ + jerry_api_object_t *global_obj_p; + jerry_api_object_t *reg_func_p; + jerry_api_value_t reg_value; + bool bok; + + global_obj_p = jerry_api_get_global (); + reg_func_p = jerry_api_create_external_function (handler); + + if (!(reg_func_p != NULL + && jerry_api_is_function (reg_func_p) + && jerry_api_is_constructor (reg_func_p))) + { + printf ("Error: create_external_function failed !!!\r\n"); + jerry_api_release_object (global_obj_p); + return false; + } + + jerry_api_acquire_object (reg_func_p); + reg_value.type = JERRY_API_DATA_TYPE_OBJECT; + reg_value.u.v_object = reg_func_p; + + bok = jerry_api_set_object_field_value (global_obj_p, + (jerry_api_char_t *) name, + ®_value); + + jerry_api_release_value (®_value); + jerry_api_release_object (reg_func_p); + jerry_api_release_object (global_obj_p); + + if (!bok) + { + printf ("Error: register_native_function failed: [%s]\r\n", name); + } + + return bok; +} + +void js_register_functions (void) +{ + REGISTER_HANDLER (assert); + REGISTER_HANDLER (led); +} diff --git a/targets/mbedstm32f4/source/jerry_extapi.h b/targets/mbedstm32f4/source/jerry_extapi.h new file mode 100644 index 0000000000..5359318b63 --- /dev/null +++ b/targets/mbedstm32f4/source/jerry_extapi.h @@ -0,0 +1,38 @@ +/* Copyright 2015 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. + */ + +#ifndef __JERRY_EXTAPI_H__ +#define __JERRY_EXTAPI_H__ + +#define JERRY_STANDALONE_EXIT_CODE_OK (0) +#define JERRY_STANDALONE_EXIT_CODE_FAIL (1) + +#define API_DATA_IS_OBJECT(val_p) \ + ((val_p)->type == JERRY_API_DATA_TYPE_OBJECT) + +#define API_DATA_IS_FUNCTION(val_p) \ + (API_DATA_IS_OBJECT (val_p) && \ + jerry_api_is_function ((val_p)->u.v_object)) + +#define JS_VALUE_TO_NUMBER(val_p) \ + ((val_p)->type == JERRY_API_DATA_TYPE_FLOAT32 ? \ + (double) ((val_p)->u.v_float32) : \ + (val_p)->type == JERRY_API_DATA_TYPE_FLOAT64 ? \ + (double) ((val_p)->u.v_float64) : \ + (double) ((val_p)->u.v_uint32)) + +void js_register_functions (void); + +#endif diff --git a/targets/mbedstm32f4/source/jerry_port.c b/targets/mbedstm32f4/source/jerry_port.c new file mode 100644 index 0000000000..821d5f133f --- /dev/null +++ b/targets/mbedstm32f4/source/jerry_port.c @@ -0,0 +1,55 @@ +/* Copyright 2014-2015 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 "jerry-core/jerry.h" + +/** + * Provide log message to filestream implementation for the engine. + */ +int jerry_port_logmsg (FILE *stream, /**< stream pointer */ + const char *format, /**< format string */ + ...) /**< parameters */ +{ + va_list args; + int count; + va_start (args, format); + count = vfprintf (stream, format, args); + va_end (args); + return count; +} /* jerry_port_logmsg */ + +/** + * Provide error message to console implementation for the engine. + */ +int jerry_port_errormsg (const char *format, /**< format string */ + ...) /**< parameters */ +{ + va_list args; + int count; + va_start (args, format); + count = vfprintf (stderr, format, args); + va_end (args); + return count; +} /* jerry_port_errormsg */ + +/** + * Provide output character to console implementation for the engine. + */ +int jerry_port_putchar (int c) /**< character to put */ +{ + return putchar ((unsigned char) c); +} /* jerry_port_putchar */ diff --git a/targets/mbedstm32f4/source/jerry_run.cpp b/targets/mbedstm32f4/source/jerry_run.cpp new file mode 100644 index 0000000000..a4fdee724a --- /dev/null +++ b/targets/mbedstm32f4/source/jerry_run.cpp @@ -0,0 +1,125 @@ +/* Copyright 2014-2015 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 "jerry-core/jerry.h" + +#include +#include + +#include "jerry_run.h" +#include "jerry_extapi.h" + +static const char* fn_sys_loop_name = "sysloop"; + +int js_entry (const char *source_p, const size_t source_size) +{ + const jerry_api_char_t *jerry_src = (const jerry_api_char_t *) source_p; + jerry_completion_code_t ret_code = JERRY_COMPLETION_CODE_OK; + jerry_flag_t flags = JERRY_FLAG_EMPTY; + jerry_api_object_t *err_obj_p = NULL; + + jerry_init (flags); + + js_register_functions (); + + if (!jerry_parse (jerry_src, source_size, &err_obj_p)) + { + printf ("Error: jerry_parse failed\r\n"); + ret_code = JERRY_COMPLETION_CODE_UNHANDLED_EXCEPTION; + jerry_api_release_object (err_obj_p); + } + else + { + if ((flags & JERRY_FLAG_PARSE_ONLY) == 0) + { + ret_code = jerry_run (&err_obj_p); + if (err_obj_p != NULL) + { + jerry_api_release_object (err_obj_p); + } + } + } + + return ret_code; +} + +int js_eval (const char *source_p, const size_t source_size) +{ + jerry_completion_code_t status; + jerry_api_value_t res; + + status = jerry_api_eval ((jerry_api_char_t *) source_p, + source_size, + false, + false, + &res); + + jerry_api_release_value (&res); + + return status; +} + +int js_loop (uint32_t ticknow) +{ + jerry_api_object_t *global_obj_p; + jerry_api_value_t sysloop_func; + bool is_ok; + + global_obj_p = jerry_api_get_global (); + is_ok = jerry_api_get_object_field_value (global_obj_p, + (const jerry_api_char_t*) fn_sys_loop_name, + &sysloop_func); + if (!is_ok) + { + printf ("Error: '%s' not defined!!!\r\n", fn_sys_loop_name); + jerry_api_release_object (global_obj_p); + return -1; + } + + if (!API_DATA_IS_FUNCTION (&sysloop_func)) + { + printf ("Error: '%s' is not a function!!!\r\n", fn_sys_loop_name); + jerry_api_release_value (&sysloop_func); + jerry_api_release_object (global_obj_p); + return -2; + } + + jerry_api_value_t* val_args; + uint16_t val_argv; + + val_argv = 1; + val_args = (jerry_api_value_t*) malloc (sizeof (jerry_api_value_t) * val_argv); + val_args[0].type = JERRY_API_DATA_TYPE_UINT32; + val_args[0].u.v_uint32 = ticknow; + + jerry_api_value_t res; + is_ok = jerry_api_call_function (sysloop_func.u.v_object, + global_obj_p, + &res, + val_args, + val_argv); + jerry_api_release_value (&res); + free (val_args); + + jerry_api_release_value (&sysloop_func); + jerry_api_release_object (global_obj_p); + + return 0; +} + +void js_exit (void) +{ + jerry_cleanup (); +} diff --git a/targets/mbedstm32f4/source/jerry_run.h b/targets/mbedstm32f4/source/jerry_run.h new file mode 100644 index 0000000000..f595b0e9a5 --- /dev/null +++ b/targets/mbedstm32f4/source/jerry_run.h @@ -0,0 +1,24 @@ +/* Copyright 2015 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. + */ + +#ifndef __JERRY_RUN_H__ +#define __JERRY_RUN_H__ + +int js_entry (const char *source_p, const size_t source_size); +int js_eval (const char *source_p, const size_t source_size); +int js_loop (uint32_t ticknow); +void js_exit (void); + +#endif diff --git a/targets/mbedstm32f4/source/main.cpp b/targets/mbedstm32f4/source/main.cpp new file mode 100644 index 0000000000..86cf7f52bf --- /dev/null +++ b/targets/mbedstm32f4/source/main.cpp @@ -0,0 +1,75 @@ +/* Copyright 2014-2015 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 "mbed-drivers/mbed.h" + +#include "jerry-core/jerry.h" +#include "jerry_run.h" + +//----------------------------------------------------------------------------- + +#include "jerry_targetjs.h" + +static int jerry_init (void) +{ + int retcode; + int src; + + DECLARE_JS_CODES; + + /* run main.js */ + retcode = js_entry (js_codes[0].source, js_codes[0].length); + if (retcode != 0) + { + pc.printf ("js_entry failed code(%d) [%s]\r\n", retcode, js_codes[0].name); + js_exit (); + return -1; + } + /* run rest of the js files */ + for (src = 1; js_codes[src].source; src++) { + retcode = js_eval (js_codes[src].source, js_codes[src].length); + if (retcode != 0) + { + pc.printf ("js_eval failed code(%d) [%s]\r\n", retcode, js_codes[src].name); + js_exit (); + return -2; + } + } + return 0; +} + +static void jerry_loop (void) +{ + static uint32_t _jcount = 0; + + js_loop (_jcount++); +} + + +void app_start (int, char**) +{ + // set 9600 baud rate for stdout + static Serial pc (USBTX, USBRX); + pc.baud (9600); + + pc.printf ("\r\nJerryScript in mbed K64F\r\n"); + pc.printf (" build %s\r\n", jerry_build_date); + pc.printf (" hash %s\r\n", jerry_commit_hash); + pc.printf (" branch %s\r\n", jerry_branch_name); + + if (jerry_init () == 0) { + minar::Scheduler::postCallback(jerry_loop).period(minar::milliseconds(100)); + } +} diff --git a/targets/mbedstm32f4/source/makejerry.cmake b/targets/mbedstm32f4/source/makejerry.cmake new file mode 100644 index 0000000000..90a9efe56a --- /dev/null +++ b/targets/mbedstm32f4/source/makejerry.cmake @@ -0,0 +1,30 @@ +# Copyright 2015 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. + +# application name +set(MBEDMODULE "jerry") + +# add include jerry-core +set(LJCORE ${CMAKE_CURRENT_LIST_DIR}/../../../) +include_directories(${LJCORE}) + +# compile flags +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mlittle-endian -mthumb -mcpu=cortex-m4") + +# link jerryscript +set(LJPATH ${CMAKE_CURRENT_LIST_DIR}/../libjerry) +set(LJFILES "") +set(LJFILES ${LJFILES} ${LJPATH}/libfdlibm.a) +set(LJFILES ${LJFILES} ${LJPATH}/libjerrycore.a) +target_link_libraries(${MBEDMODULE} ${LJFILES}) diff --git a/targets/mbedstm32f4/source/native_mbedstm32f4.cpp b/targets/mbedstm32f4/source/native_mbedstm32f4.cpp new file mode 100644 index 0000000000..d7ed34dc4a --- /dev/null +++ b/targets/mbedstm32f4/source/native_mbedstm32f4.cpp @@ -0,0 +1,25 @@ +/* Copyright 2014-2015 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 "mbed-drivers/mbed.h" + +#include "native_mbedstm32f4.h" + +void native_led (int port, int val) +{ + static const PinName portmap[] = { LED1, LED2, LED3, LED4 }; + DigitalOut led (portmap[port]); + led = val; +} diff --git a/targets/mbedstm32f4/source/native_mbedstm32f4.h b/targets/mbedstm32f4/source/native_mbedstm32f4.h new file mode 100644 index 0000000000..c6ba6ad334 --- /dev/null +++ b/targets/mbedstm32f4/source/native_mbedstm32f4.h @@ -0,0 +1,21 @@ +/* Copyright 2014-2015 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. + */ + +#ifndef __NATIVE_MBEDSTM32F4_H__ +#define __NATIVE_MBEDSTM32F4_H__ + +void native_led (int port, int val); + +#endif From e3989f67819ea05b74f6e77dea9c49dffef491b9 Mon Sep 17 00:00:00 2001 From: Imre Kiss Date: Mon, 11 Apr 2016 13:52:41 +0200 Subject: [PATCH 2/3] Fix coding style in the JavaScript files. JerryScript-DCO-1.0-Signed-off-by: Imre Kiss kissi@inf.u-szeged.hu --- targets/mbedstm32f4/js/blink.js | 7 ++++--- targets/mbedstm32f4/js/main.js | 7 ++++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/targets/mbedstm32f4/js/blink.js b/targets/mbedstm32f4/js/blink.js index 0e51261393..49d8f861eb 100644 --- a/targets/mbedstm32f4/js/blink.js +++ b/targets/mbedstm32f4/js/blink.js @@ -1,9 +1,10 @@ var check = 1; -function blink() { +function blink () +{ var blk = (check > 8) ? 1 : 0; led(1, blk); - check = (check >= 10) ? 1 : check+1; + check = (check >= 10) ? 1 : check + 1; } -print("blink js OK"); +print ("blink js OK"); diff --git a/targets/mbedstm32f4/js/main.js b/targets/mbedstm32f4/js/main.js index 7063198045..e9380ba7fb 100644 --- a/targets/mbedstm32f4/js/main.js +++ b/targets/mbedstm32f4/js/main.js @@ -1,5 +1,6 @@ -function sysloop(ticknow) { - blink(); +function sysloop (ticknow) +{ + blink (); } -print("main js OK"); +print ("main js OK"); From c8764f0c689cc11750f44c60f5222ce754d6d140 Mon Sep 17 00:00:00 2001 From: Imre Kiss Date: Mon, 18 Apr 2016 09:42:17 +0200 Subject: [PATCH 3/3] Declare serial variable for communication. JerryScript-DCO-1.0-Signed-off-by: Imre Kiss kissi@inf.u-szeged.hu --- targets/mbedstm32f4/source/main.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/targets/mbedstm32f4/source/main.cpp b/targets/mbedstm32f4/source/main.cpp index 86cf7f52bf..1432d9f5cd 100644 --- a/targets/mbedstm32f4/source/main.cpp +++ b/targets/mbedstm32f4/source/main.cpp @@ -22,6 +22,8 @@ #include "jerry_targetjs.h" +static Serial pc (USBTX, USBRX); //TR, RX + static int jerry_init (void) { int retcode; @@ -61,7 +63,6 @@ static void jerry_loop (void) void app_start (int, char**) { // set 9600 baud rate for stdout - static Serial pc (USBTX, USBRX); pc.baud (9600); pc.printf ("\r\nJerryScript in mbed K64F\r\n");