diff options
Diffstat (limited to 'targets/baremetal-sdk/esp8266-rtos-sdk/user')
5 files changed, 555 insertions, 0 deletions
diff --git a/targets/baremetal-sdk/esp8266-rtos-sdk/user/Makefile b/targets/baremetal-sdk/esp8266-rtos-sdk/user/Makefile new file mode 100644 index 00000000..76aea693 --- /dev/null +++ b/targets/baremetal-sdk/esp8266-rtos-sdk/user/Makefile @@ -0,0 +1,44 @@ + +############################################################# +# Required variables for each makefile +# Discard this section from all parent makefiles +# Expected variables (with automatic defaults): +# CSRCS (all "C" files in the dir) +# SUBDIRS (all subdirs with a Makefile) +# GEN_LIBS - list of libs to be generated () +# GEN_IMAGES - list of images to be generated () +# COMPONENTS_xxx - a list of libs/objs in the form +# subdir/lib to be extracted and rolled up into +# a generated lib/image xxx.a () +# +ifndef PDIR +GEN_LIBS = libuser.a +endif + + +############################################################# +# Configuration i.e. compile options etc. +# Target specific stuff (defines etc.) goes in here! +# Generally values applying to a tree are captured in the +# makefile at its root level - these are then overridden +# for a subtree within the makefile rooted therein +# +#DEFINES += + +############################################################# +# Recursion Magic - Don't touch this!! +# +# Each subtree potentially has an include directory +# corresponding to the common APIs applicable to modules +# rooted at that subtree. Accordingly, the INCLUDE PATH +# of a module can only contain the include directories up +# its parent path, and not its siblings +# +# Required for each makefile to inherit from the parent +# + + +INCLUDES := $(INCLUDES) -I $(PDIR)include +INCLUDES += -I ./ +PDIR := ../$(PDIR) +sinclude $(PDIR)Makefile diff --git a/targets/baremetal-sdk/esp8266-rtos-sdk/user/jerry_extapi.c b/targets/baremetal-sdk/esp8266-rtos-sdk/user/jerry_extapi.c new file mode 100644 index 00000000..5cccce4d --- /dev/null +++ b/targets/baremetal-sdk/esp8266-rtos-sdk/user/jerry_extapi.c @@ -0,0 +1,182 @@ +/* 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 "jerry_extapi.h" + +#include <stdio.h> +#include <stdlib.h> + +#include "jerryscript.h" + +#include "c_types.h" +#include "gpio.h" + +#define __UNUSED__ __attribute__ ((unused)) + +#define DELCARE_HANDLER(NAME) \ + static jerry_value_t NAME##_handler (const jerry_call_info_t *call_info_p __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_true (args_p[0])) + { + printf (">> Jerry assert true\r\n"); + return jerry_boolean (true); + } + printf ("Script assertion failed\n"); + exit (JERRY_STANDALONE_EXIT_CODE_FAIL); + return jerry_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_string_size (args_p[0], JERRY_ENCODING_UTF8); + char *buffer; + buffer = (char *) malloc (size + 1); + + if (!buffer) + { + // not enough memory for this string. + printf ("[<too-long-string>]"); + continue; + } + + jerry_string_to_buffer (args_p[cc], JERRY_ENCODING_UTF8, (jerry_char_t *) buffer, size); + buffer[size] = '\0'; + printf ("%s ", buffer); + free (buffer); + } + else if (jerry_value_is_number (args_p[cc])) + { + double number = jerry_value_as_number (args_p[cc]); + if ((int) number == number) + { + printf ("%d", (int) number); + } + else + { + char buff[50]; + sprintf (buff, "%.10f", number); + printf ("%s", buff); + } + } + } + printf ("\r\n"); + } + return jerry_boolean (true); +} /* print */ + +DELCARE_HANDLER (gpio_dir) +{ + if (args_cnt < 2) + { + return jerry_boolean (false); + } + + int port = (int) jerry_value_as_number (args_p[0]); + int value = (int) jerry_value_as_number (args_p[1]); + + if (value) + { + GPIO_AS_OUTPUT (1 << port); + } + else + { + GPIO_AS_INPUT (1 << port); + } + + return jerry_boolean (true); +} /* gpio_dir */ + +DELCARE_HANDLER (gpio_set) +{ + if (args_cnt < 2) + { + return jerry_boolean (false); + } + + int port = (int) jerry_value_as_number (args_p[0]); + int value = (int) jerry_value_as_number (args_p[1]); + + GPIO_OUTPUT_SET (port, value); + + return jerry_boolean (true); +} /* gpio_set */ + +DELCARE_HANDLER (gpio_get) +{ + if (args_cnt < 1) + { + return jerry_boolean (false); + } + + int port = (int) jerry_value_as_number (args_p[0]); + int value = GPIO_INPUT_GET (port) ? 1 : 0; + + return jerry_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_current_realm (); + jerry_value_t reg_func_val = jerry_function_external (handler); + + if (!(jerry_value_is_function (reg_func_val) && jerry_value_is_constructor (reg_func_val))) + { + printf ("!!! create_external_function failed !!!\r\n"); + jerry_value_free (reg_func_val); + jerry_value_free (global_obj_val); + return false; + } + + jerry_value_t prop_name_val = jerry_string_sz (name); + jerry_value_t res = jerry_object_set (global_obj_val, prop_name_val, reg_func_val); + + jerry_value_free (reg_func_val); + jerry_value_free (global_obj_val); + jerry_value_free (prop_name_val); + + if (jerry_value_is_exception (res)) + { + printf ("!!! register_native_function failed: [%s]\r\n", name); + jerry_value_free (res); + return false; + } + + jerry_value_free (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 */ diff --git a/targets/baremetal-sdk/esp8266-rtos-sdk/user/jerry_port.c b/targets/baremetal-sdk/esp8266-rtos-sdk/user/jerry_port.c new file mode 100644 index 00000000..f4d58769 --- /dev/null +++ b/targets/baremetal-sdk/esp8266-rtos-sdk/user/jerry_port.c @@ -0,0 +1,73 @@ +/* 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 <stdio.h> +#include <stdarg.h> +#include <sys/time.h> + +#include "esp_common.h" + +#include "jerryscript-port.h" + +/** + * Provide log message implementation for the engine. + */ +void +jerry_port_log (jerry_log_level_t level, /**< log level */ + const char *format, /**< format string */ + ...) /**< parameters */ +{ + (void) level; /* ignore log level */ + + va_list args; + va_start (args, format); + vfprintf (stderr, format, args); + va_end (args); +} /* jerry_port_log */ + +/** + * Provide fatal message implementation for the engine. + */ +void +jerry_port_fatal (jerry_fatal_code_t code) +{ + jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Jerry Fatal Error!\n"); + while (true); +} /* jerry_port_fatal */ + +/** + * Implementation of jerry_port_get_current_time. + * + * @return current timer's counter value in milliseconds + */ +double +jerry_port_get_current_time (void) +{ + uint32_t rtc_time = system_rtc_clock_cali_proc(); + return (double) rtc_time; +} /* jerry_port_get_current_time */ + +/** + * Dummy function to get the time zone adjustment. + * + * @return 0 + */ +double +jerry_port_get_local_time_zone_adjustment (double unix_ms, bool is_utc) +{ + /* We live in UTC. */ + return 0; +} /* jerry_port_get_local_time_zone_adjustment */ diff --git a/targets/baremetal-sdk/esp8266-rtos-sdk/user/jerry_run.c b/targets/baremetal-sdk/esp8266-rtos-sdk/user/jerry_run.c new file mode 100644 index 00000000..b3205bac --- /dev/null +++ b/targets/baremetal-sdk/esp8266-rtos-sdk/user/jerry_run.c @@ -0,0 +1,109 @@ +/* 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 "jerry_run.h" + +#include <stdio.h> +#include <stdlib.h> + +#include "jerryscript-port.h" +#include "jerryscript.h" + +#include "jerry_extapi.h" + +static const char *fn_sys_loop_name = "sysloop"; + +void +js_entry () +{ + union + { + double d; + unsigned u; + } now = { .d = jerry_port_get_current_time () }; + srand (now.u); + + 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, JERRY_PARSE_NO_OPTS); + if (jerry_value_is_exception (res)) + { + jerry_value_free (res); + return -1; + } + + jerry_value_free (res); + + return 0; +} + +int +js_loop (uint32_t ticknow) +{ + jerry_value_t global_obj_val = jerry_current_realm (); + jerry_value_t prop_name_val = jerry_string_sz (fn_sys_loop_name); + jerry_value_t sysloop_func = jerry_object_get (global_obj_val, prop_name_val); + jerry_value_free (prop_name_val); + + if (jerry_value_is_exception (sysloop_func)) + { + printf ("Error: '%s' not defined!!!\r\n", fn_sys_loop_name); + jerry_value_free (sysloop_func); + jerry_value_free (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_value_free (sysloop_func); + jerry_value_free (global_obj_val); + return -2; + } + + jerry_value_t val_args[] = { jerry_number (ticknow) }; + uint16_t val_argv = sizeof (val_args) / sizeof (jerry_value_t); + + jerry_value_t res = jerry_call (sysloop_func, global_obj_val, val_args, val_argv); + + for (uint16_t i = 0; i < val_argv; i++) + { + jerry_value_free (val_args[i]); + } + + jerry_value_free (sysloop_func); + jerry_value_free (global_obj_val); + + if (jerry_value_is_exception (res)) + { + jerry_value_free (res); + return -3; + } + + jerry_value_free (res); + + return 0; +} + +void +js_exit (void) +{ + jerry_cleanup (); +} diff --git a/targets/baremetal-sdk/esp8266-rtos-sdk/user/user_main.c b/targets/baremetal-sdk/esp8266-rtos-sdk/user/user_main.c new file mode 100644 index 00000000..48fd2631 --- /dev/null +++ b/targets/baremetal-sdk/esp8266-rtos-sdk/user/user_main.c @@ -0,0 +1,147 @@ +/* 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 "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "uart.h" + +#include "user_config.h" +#include "jerry_run.h" +#include "jerry-targetjs.h" + +static void show_free_mem(int idx) { + size_t res = xPortGetFreeHeapSize(); + printf("dbg free memory(%d): %d\r\n", idx, res); +} + +static int jerry_task_init(void) { + DECLARE_JS_CODES; + + js_entry(); + + /* run rest of the js files first */ + show_free_mem(2); + for (int src = 1; 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 -1; + } + } + + /* run main.js */ + int retcode = js_eval(js_codes[0].source, js_codes[0].length); + if (retcode != 0) { + printf("js_eval failed code(%d) [%s]\r\n", retcode, js_codes[0].name); + return -2; + } + show_free_mem(3); + return 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(4); + } + ticknow++; + } + } + js_exit(); +} + +/* + * This is entry point for user code + */ +void ICACHE_FLASH_ATTR user_init(void) +{ + UART_SetBaudrate(UART0, BIT_RATE_115200); + + show_free_mem(0); + wifi_softap_dhcps_stop(); + show_free_mem(1); + + PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO0_U, FUNC_GPIO0); // GPIO 0 + PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO2_U, FUNC_GPIO2); // GPIO 2 + + 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; +} |