Merged target for mbed boards.

Merged the mbed releated targets into one source.
Available targets: FRDM-K64f, Discovery-STM32F4, Discovery-STM32F429ZI, NUCLEO-STM32F4

JerryScript-DCO-1.0-Signed-off-by: Imre Kiss kissi@inf.u-szeged.hu
This commit is contained in:
Imre Kiss
2016-04-19 14:04:18 +02:00
parent 94161d350a
commit 5369fd4515
20 changed files with 301 additions and 299 deletions
+130
View File
@@ -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 <stdlib.h>
#include <stdio.h>
#include "jerry-core/jerry.h"
#include "jerry_extapi.h"
#include "native_mbed.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,
&reg_value);
jerry_api_release_value (&reg_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);
}
+38
View File
@@ -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 ? \
static_cast<double>((val_p)->u.v_float32) : \
(val_p)->type == JERRY_API_DATA_TYPE_FLOAT64 ? \
static_cast<double>((val_p)->u.v_float64) : \
static_cast<double>((val_p)->u.v_uint32))
void js_register_functions (void);
#endif
+128
View File
@@ -0,0 +1,128 @@
/* Copyright 2014-2015 Samsung Electronics Co., Ltd.
* Copyright 2016 University of Szeged.
*
* 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 <stdio.h>
#include "jerry-core/jerry.h"
#include "jerry_extapi.h"
#include "jerry_run.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_api_value_t err_value = jerry_api_create_void_value ();
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_value);
jerry_api_string_t *err_str_p = NULL;
if (err_str_p != NULL)
{
jerry_api_release_string (err_str_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 ();
}
+24
View File
@@ -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
+75
View File
@@ -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 Serial pc (USBTX, USBRX); //tx, rx
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)
{
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)
{
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
pc.baud (9600);
printf ("\r\nJerryScript in mbed\r\n");
printf (" build %s\r\n", jerry_build_date);
printf (" hash %s\r\n", jerry_commit_hash);
printf (" branch %s\r\n", jerry_branch_name);
if (jerry_init () == 0)
{
minar::Scheduler::postCallback(jerry_loop).period(minar::milliseconds(100));
}
}
+30
View File
@@ -0,0 +1,30 @@
# Copyright 2015-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.
# 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}/libjerrylibm.a)
set(LJFILES ${LJFILES} ${LJPATH}/libjerrycore.a)
target_link_libraries(${MBEDMODULE} ${LJFILES})
+25
View File
@@ -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_mbed.h"
void native_led (int port, int val)
{
static const PinName portmap[] = { LED1, LED2, LED3, LED4 };
static DigitalOut led (portmap[port]);
led = val;
}
+21
View File
@@ -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_MBED_H__
#define __NATIVE_MBED_H__
void native_led (int port, int val);
#endif /* !__NATIVE_MBED_H__ */
+87
View File
@@ -0,0 +1,87 @@
/* Copyright 2016 University of Szeged.
*
* 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.
*/
#define _BSD_SOURCE
#include <stdarg.h>
#include <stdlib.h>
#include <sys/time.h>
#include "jerry-core/jerry-port.h"
#include "mbed-hal/us_ticker_api.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 */
/**
* Implementation of jerry_port_fatal.
*/
void
jerry_port_fatal (jerry_fatal_code_t code) /**< fatal code enum item */
{
exit (code);
} /* jerry_port_fatal */
/**
* Implementation of jerry_port_get_time_zone.
*
* @return true - if success
*/
bool
jerry_port_get_time_zone (jerry_time_zone_t *tz_p) /**< timezone pointer */
{
tz_p->offset = 0;
tz_p->daylight_saving_time = 0;
return true;
} /* jerry_port_get_time_zone */
/**
* Implementation of jerry_port_get_current_time.
*
* @return current timer's counter value in microseconds
*/
double
jerry_port_get_current_time ()
{
return (double) us_ticker_read ();
} /* jerry_port_get_current_time */