Revive target: ESP8266

This patch updates ESP8266 build system and code base as well.
 - Removed unnecessary files.
 - Decreased code size.
 - Refactored jerry_{port, extapi, run}.c to make it more easy to handle.
 - Readme.md is updated as well which contains detailed step by step description about how to set up environment and use JerryScript.

Finally, it solves the related issue #1375.

JerryScript-DCO-1.0-Signed-off-by: Robert Fancsik frobert@inf.u-szeged.hu
This commit is contained in:
Robert Fancsik
2017-10-02 13:47:36 +02:00
committed by yichoi
parent bdcd2d8179
commit c3a9821171
18 changed files with 336 additions and 927 deletions
+4 -3
View File
@@ -37,7 +37,8 @@ endif
# Required for each makefile to inherit from the parent
#
INCLUDES := $(INCLUDES) -I $(PDIR)include
INCLUDES += -I ./ -I ../include -I../../../
sinclude $(PDIR)Makefile
INCLUDES := $(INCLUDES) -I $(PDIR)include
INCLUDES += -I ./
PDIR := ../$(PDIR)
sinclude $(PDIR)Makefile
+177
View File
@@ -0,0 +1,177 @@
/* 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 <stdio.h>
#include "c_types.h"
#include "gpio.h"
#include "jerryscript.h"
#include "jerry_extapi.h"
#define __UNUSED__ __attribute__((unused))
#define DELCARE_HANDLER(NAME) \
static jerry_value_t \
NAME ## _handler (const jerry_value_t function_obj_val __UNUSED__, \
const jerry_value_t this_val __UNUSED__, \
const jerry_value_t args_p[], \
const jerry_length_t args_cnt)
#define REGISTER_HANDLER(NAME) \
register_native_function ( # NAME, NAME ## _handler)
DELCARE_HANDLER(assert) {
if (args_cnt == 1
&& jerry_value_is_boolean (args_p[0])
&& jerry_get_boolean_value (args_p[0]))
{
printf (">> Jerry assert true\r\n");
return jerry_create_boolean (true);
}
printf ("Script assertion failed\n");
exit (JERRY_STANDALONE_EXIT_CODE_FAIL);
return jerry_create_boolean (false);
} /* assert */
DELCARE_HANDLER(print) {
if (args_cnt)
{
for (jerry_length_t cc = 0; cc < args_cnt; cc++)
{
if (jerry_value_is_string (args_p[cc]))
{
jerry_size_t size = jerry_get_string_size (args_p[0]);
char *buffer;
buffer = (char *) malloc(size + 1);
if(!buffer)
{
// not enough memory for this string.
printf("[<too-long-string>]");
continue;
}
jerry_string_to_char_buffer (args_p[cc],
(jerry_char_t *) buffer,
size);
*(buffer + size) = 0;
printf("%s ", buffer);
free (buffer);
}
else
{
printf ("(%d) ", args_p[cc]);
}
}
printf ("\r\n");
}
return jerry_create_boolean (true);
} /* print */
DELCARE_HANDLER(gpio_dir) {
if (args_cnt < 2)
{
return jerry_create_boolean (false);
}
int port = (int) jerry_get_number_value (args_p[0]);
int value = (int) jerry_get_number_value (args_p[1]);
if (value)
{
GPIO_AS_OUTPUT(1 << port);
}
else
{
GPIO_AS_INPUT(1 << port);
}
return jerry_create_boolean (true);
} /* gpio_dir */
DELCARE_HANDLER(gpio_set) {
if (args_cnt < 2)
{
return jerry_create_boolean (false);
}
int port = (int) jerry_get_number_value (args_p[0]);
int value = (int) jerry_get_number_value (args_p[1]);
GPIO_OUTPUT_SET(port, value);
return jerry_create_boolean (true);
} /* gpio_set */
DELCARE_HANDLER(gpio_get) {
if (args_cnt < 1)
{
return jerry_create_boolean (false);
}
int port = (int) jerry_get_number_value (args_p[0]);
int value = GPIO_INPUT_GET(port) ? 1 : 0;
return jerry_create_number ((double) value);
} /* gpio_get */
static bool
register_native_function (const char* name,
jerry_external_handler_t handler)
{
jerry_value_t global_obj_val = jerry_get_global_object ();
jerry_value_t reg_func_val = jerry_create_external_function (handler);
bool bok = true;
if (!(jerry_value_is_function (reg_func_val)
&& jerry_value_is_constructor (reg_func_val)))
{
printf ("!!! create_external_function failed !!!\r\n");
jerry_release_value (reg_func_val);
jerry_release_value (global_obj_val);
return false;
}
jerry_value_t prop_name_val = jerry_create_string ((const jerry_char_t *) name);
jerry_value_t res = jerry_set_property (global_obj_val, prop_name_val, reg_func_val);
jerry_release_value (reg_func_val);
jerry_release_value (global_obj_val);
jerry_release_value (prop_name_val);
if (jerry_value_has_error_flag (res))
{
printf ("!!! register_native_function failed: [%s]\r\n", name);
jerry_release_value (res);
return false;
}
jerry_release_value (res);
return true;
} /* register_native_function */
void js_register_functions (void)
{
REGISTER_HANDLER(assert);
REGISTER_HANDLER(print);
REGISTER_HANDLER(gpio_dir);
REGISTER_HANDLER(gpio_set);
REGISTER_HANDLER(gpio_get);
} /* js_register_functions */
+37 -36
View File
@@ -13,12 +13,14 @@
* limitations under the License.
*/
#include "esp_common.h"
#include <stdio.h>
#include <stdarg.h>
#include <sys/time.h>
#include "jerry-core/include/jerryscript-port.h"
int ets_putc (int);
#include "esp_common.h"
#include "jerryscript-port.h"
/**
* Provide log message implementation for the engine.
@@ -32,50 +34,49 @@ jerry_port_log (jerry_log_level_t level, /**< log level */
va_list args;
va_start (args, format);
/* TODO, uncomment when vprint link is ok */
/* vprintf (stderr, format, args); */
vfprintf (stderr, format, args);
va_end (args);
} /* jerry_port_log */
/** exit - cause normal process termination */
void exit (int status)
/**
* Provide fatal message implementation for the engine.
*/
void
jerry_port_fatal (jerry_fatal_code_t code)
{
while (true)
{
}
} /* exit */
/** abort - cause abnormal process termination */
void abort (void)
{
while (true)
{
}
} /* abort */
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Jerry Fatal Error!\n");
while (true);
} /* jerry_port_fatal */
/**
* fwrite
* Implementation of jerry_port_get_current_time.
*
* @return number of bytes written
* @return current timer's counter value in milliseconds
*/
size_t
fwrite (const void *ptr, /**< data to write */
size_t size, /**< size of elements to write */
size_t nmemb, /**< number of elements */
FILE *stream) /**< stream pointer */
double
jerry_port_get_current_time (void)
{
return size * nmemb;
} /* fwrite */
struct timeval tv;
if (gettimeofday (&tv, NULL) != 0)
{
return 0;
}
return ((double) tv.tv_sec) * 1000.0 + ((double) tv.tv_usec) / 1000.0;
} /* jerry_port_get_current_time */
/**
* This function can get the time as well as a timezone.
* Dummy function to get the time zone.
*
* @return 0 if success, -1 otherwise
* @return true
*/
int
gettimeofday (void *tp, /**< struct timeval */
void *tzp) /**< struct timezone */
bool
jerry_port_get_time_zone (jerry_time_zone_t *tz_p)
{
return -1;
} /* gettimeofday */
/* We live in UTC. */
tz_p->offset = 0;
tz_p->daylight_saving_time = 0;
return true;
} /* jerry_port_get_time_zone */
+99
View File
@@ -0,0 +1,99 @@
/* 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 <stdio.h>
#include "jerry_extapi.h"
#include "jerry_run.h"
#include "jerryscript.h"
#include "jerryscript-port.h"
static const char* fn_sys_loop_name = "sysloop";
void js_entry ()
{
srand ((unsigned) jerry_port_get_current_time ());
jerry_init (JERRY_INIT_EMPTY);
js_register_functions ();
}
int js_eval (const char *source_p, const size_t source_size)
{
jerry_value_t res = jerry_eval ((jerry_char_t *) source_p,
source_size,
false);
if (jerry_value_has_error_flag (res)) {
jerry_release_value (res);
return -1;
}
jerry_release_value (res);
return 0;
}
int js_loop (uint32_t ticknow)
{
jerry_value_t global_obj_val = jerry_get_global_object ();
jerry_value_t prop_name_val = jerry_create_string ((const jerry_char_t *) fn_sys_loop_name);
jerry_value_t sysloop_func = jerry_get_property (global_obj_val, prop_name_val);
jerry_release_value (prop_name_val);
if (jerry_value_has_error_flag (sysloop_func)) {
printf ("Error: '%s' not defined!!!\r\n", fn_sys_loop_name);
jerry_release_value (sysloop_func);
jerry_release_value (global_obj_val);
return -1;
}
if (!jerry_value_is_function (sysloop_func)) {
printf ("Error: '%s' is not a function!!!\r\n", fn_sys_loop_name);
jerry_release_value (sysloop_func);
jerry_release_value (global_obj_val);
return -2;
}
jerry_value_t val_args[] = { jerry_create_number (ticknow) };
uint16_t val_argv = sizeof (val_args) / sizeof (jerry_value_t);
jerry_value_t res = jerry_call_function (sysloop_func,
global_obj_val,
val_args,
val_argv);
for (uint16_t i = 0; i < val_argv; i++) {
jerry_release_value (val_args[i]);
}
jerry_release_value (sysloop_func);
jerry_release_value (global_obj_val);
if (jerry_value_has_error_flag (res)) {
jerry_release_value (res);
return -3;
}
jerry_release_value (res);
return 0;
}
void js_exit (void)
{
jerry_cleanup ();
}
-50
View File
@@ -1,50 +0,0 @@
/* 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.
*/
/******************************************************************************
* Copyright 2013-2014 Espressif Systems (Wuxi)
*
* FileName: user_main.c
*
* Description: entry file of user application
*
* Modification history:
* 2014/12/1, v1.0 create this file.
*******************************************************************************/
#include "esp_common.h"
#include "user_config.h"
#include "esp8266_gpio.h"
void native_gpio_dir(int port, int value) {
if (value) {
GPIO_AS_OUTPUT(1 << port);
}
else {
GPIO_AS_INPUT(1 << port);
}
}
void native_gpio_set(int port, int value) {
GPIO_OUTPUT_SET(port, value);
}
int native_gpio_get(int port) {
return GPIO_INPUT_GET(port);
}
-39
View File
@@ -1,39 +0,0 @@
/* 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.
*/
/*
* Copyright (C) 2014 -2016 Espressif System
*
*/
#include "esp_common.h"
#include "esp8266_gpio.h"
//-----------------------------------------------------------------------------
void gpio_output_conf(uint32 set_mask, uint32 clear_mask, uint32 enable_mask,
uint32 disable_mask) {
GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, set_mask);
GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, clear_mask);
GPIO_REG_WRITE(GPIO_ENABLE_W1TS_ADDRESS, enable_mask);
GPIO_REG_WRITE(GPIO_ENABLE_W1TC_ADDRESS, disable_mask);
}
uint32 gpio_input_get(void) {
return GPIO_REG_READ(GPIO_IN_ADDRESS);
}
+65 -36
View File
@@ -25,64 +25,47 @@
*******************************************************************************/
#include "esp_common.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "uart.h"
#include "user_config.h"
#include "esp8266_uart.h"
#include "jerry_run.h"
#include "jerry-targetjs.h"
//-----------------------------------------------------------------------------
void show_free_mem(int idx) {
static void show_free_mem(int idx) {
size_t res = xPortGetFreeHeapSize();
printf("dbg free memory(%d): %d\r\n", idx, res);
}
//-----------------------------------------------------------------------------
#include "jerry-targetjs.h"
static int jerry_task_init(void) {
int retcode;
int src;
DECLARE_JS_CODES;
/* run main.js */
js_entry();
/* run js files */
show_free_mem(2);
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);
return -1;
}
/* run rest of the js files */
show_free_mem(3);
for (src=1; js_codes[src].source; src++) {
retcode = js_eval(js_codes[src].source, js_codes[src].length);
for (int src = 0; js_codes[src].source; src++) {
int 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);
return -2;
}
}
show_free_mem(4);
show_free_mem(3);
return 0;
}
void jerry_task(void *pvParameters) {
const portTickType xDelay = 100 / portTICK_RATE_MS;
uint32_t ticknow = 0;
static void jerry_task(void *pvParameters) {
if (jerry_task_init() == 0) {
const portTickType xDelay = 100 / portTICK_RATE_MS;
uint32_t ticknow = 0;
for (;;) {
vTaskDelay(xDelay);
js_loop(ticknow);
if (!ticknow) {
show_free_mem(5);
show_free_mem(4);
}
ticknow++;
}
@@ -90,15 +73,12 @@ void jerry_task(void *pvParameters) {
js_exit();
}
//-----------------------------------------------------------------------------
/*
* This is entry point for user code
*/
void ICACHE_FLASH_ATTR user_init(void)
{
uart_div_modify(UART0, UART_CLK_FREQ / (BIT_RATE_115200));
UART_SetBaudrate(UART0, BIT_RATE_115200);
show_free_mem(0);
wifi_softap_dhcps_stop();
@@ -109,3 +89,52 @@ void ICACHE_FLASH_ATTR user_init(void)
xTaskCreate(jerry_task, "jerry", JERRY_STACK_SIZE, NULL, 2, NULL);
}
/*
* FunctionName : user_rf_cal_sector_set
* Description : SDK just reserved 4 sectors, used for rf init data and Parameters.
* We add this function to force users to set rf cal sector, since
* we don't know which sector is free in user's application.
* sector map for last several sectors : ABCCC
* A : rf cal
* B : rf init data
* C : sdk parameters
* Parameters : none
* Returns : rf cal sector
*/
uint32 user_rf_cal_sector_set(void)
{
flash_size_map size_map = system_get_flash_size_map();
uint32 rf_cal_sec = 0;
switch (size_map) {
case FLASH_SIZE_4M_MAP_256_256:
rf_cal_sec = 128 - 5;
break;
case FLASH_SIZE_8M_MAP_512_512:
rf_cal_sec = 256 - 5;
break;
case FLASH_SIZE_16M_MAP_512_512:
case FLASH_SIZE_16M_MAP_1024_1024:
rf_cal_sec = 512 - 5;
break;
case FLASH_SIZE_32M_MAP_512_512:
case FLASH_SIZE_32M_MAP_1024_1024:
rf_cal_sec = 1024 - 5;
break;
case FLASH_SIZE_64M_MAP_1024_1024:
rf_cal_sec = 2048 - 5;
break;
case FLASH_SIZE_128M_MAP_1024_1024:
rf_cal_sec = 4096 - 5;
break;
default:
rf_cal_sec = 0;
break;
}
return rf_cal_sec;
}