From feb27a5fc65c27ebe4953b63f8bde279740e5ab5 Mon Sep 17 00:00:00 2001 From: SaeHie Park Date: Tue, 8 Dec 2015 16:07:30 +0900 Subject: [PATCH] Add target build for ESP8266 board. JerryScript-DCO-1.0-Signed-off-by: SaeHie Park saehie.park@samsung.com --- .gitignore | 3 + targets/esp8266/LICENSE | 56 +++++ targets/esp8266/Makefile | 146 +++++++++++++ targets/esp8266/Makefile.esp8266 | 111 ++++++++++ .../esp8266/docs/ESP-PATCHFORJERRYSCRIPT.md | 75 +++++++ targets/esp8266/docs/ESP-PREREQUISITES.md | 150 ++++++++++++++ targets/esp8266/gen_misc.sh | 165 +++++++++++++++ targets/esp8266/include/esp8266_gpio.h | 151 ++++++++++++++ targets/esp8266/include/esp8266_uart.h | 48 +++++ targets/esp8266/include/jerry_extapi.h | 49 +++++ targets/esp8266/include/jerry_run.h | 34 +++ targets/esp8266/include/native_esp8266.h | 33 +++ targets/esp8266/include/user_config.h | 24 +++ targets/esp8266/js/blink.js | 15 ++ targets/esp8266/js/main.js | 4 + targets/esp8266/js2c.py | 144 +++++++++++++ targets/esp8266/readme.md | 67 ++++++ targets/esp8266/source/jerry_extapi.cpp | 195 ++++++++++++++++++ targets/esp8266/source/jerry_run.cpp | 122 +++++++++++ targets/esp8266/user/Makefile | 44 ++++ targets/esp8266/user/jerry_port.c | 66 ++++++ targets/esp8266/user/native_esp8266.c | 50 +++++ targets/esp8266/user/user_gpio.c | 39 ++++ targets/esp8266/user/user_main.c | 112 ++++++++++ 24 files changed, 1903 insertions(+) create mode 100644 targets/esp8266/LICENSE create mode 100644 targets/esp8266/Makefile create mode 100644 targets/esp8266/Makefile.esp8266 create mode 100644 targets/esp8266/docs/ESP-PATCHFORJERRYSCRIPT.md create mode 100644 targets/esp8266/docs/ESP-PREREQUISITES.md create mode 100644 targets/esp8266/gen_misc.sh create mode 100644 targets/esp8266/include/esp8266_gpio.h create mode 100644 targets/esp8266/include/esp8266_uart.h create mode 100644 targets/esp8266/include/jerry_extapi.h create mode 100644 targets/esp8266/include/jerry_run.h create mode 100644 targets/esp8266/include/native_esp8266.h create mode 100644 targets/esp8266/include/user_config.h create mode 100644 targets/esp8266/js/blink.js create mode 100644 targets/esp8266/js/main.js create mode 100755 targets/esp8266/js2c.py create mode 100644 targets/esp8266/readme.md create mode 100644 targets/esp8266/source/jerry_extapi.cpp create mode 100644 targets/esp8266/source/jerry_run.cpp create mode 100644 targets/esp8266/user/Makefile create mode 100644 targets/esp8266/user/jerry_port.c create mode 100644 targets/esp8266/user/native_esp8266.c create mode 100644 targets/esp8266/user/user_gpio.c create mode 100644 targets/esp8266/user/user_main.c diff --git a/.gitignore b/.gitignore index 31d226e8b..f398f8f9e 100644 --- a/.gitignore +++ b/.gitignore @@ -41,3 +41,6 @@ targets/mbedk64f/libjerry targets/mbedk64f/build targets/mbedk64f/yotta_modules targets/mbedk64f/yotta_targets +.output +targets/esp8266/output.map +targets/esp8266/libs diff --git a/targets/esp8266/LICENSE b/targets/esp8266/LICENSE new file mode 100644 index 000000000..31c1f3f14 --- /dev/null +++ b/targets/esp8266/LICENSE @@ -0,0 +1,56 @@ +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. + +================================================================================ + +ESPRSSIF MIT License + +Copyright (c) 2015 + +Permission is hereby granted for use on ESPRESSIF SYSTEMS ESP8266 only, in which case, +it is free of charge, to any person obtaining a copy of this software and associated +documentation files (the ¡°Software¡±), to deal in the Software without restriction, +including without limitation the rights to use, copy, modify, merge, publish, distribute, +sublicense, and/or sell copies of the Software, and to permit persons to whom the Software +is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or +substantial portions of the Software. + +THE SOFTWARE IS PROVIDED ¡°AS IS¡±, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, +INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR +PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE +FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. + + +ÀÖöÎ MIT Ðí¿ÉÖ¤ + +°æÈ¨ (c) 2015 <ÀÖöÎÐÅÏ¢¿Æ¼¼£¨ÉϺ££©ÓÐÏÞ¹«Ë¾> + +¸ÃÐí¿ÉÖ¤ÊÚȨ½öÏÞÓÚÀÖöÎÐÅÏ¢¿Æ¼¼ ESP8266 ²úÆ·µÄÓ¦Óÿª·¢¡£ÔÚ´ËÇé¿öÏ£¬¸ÃÐí¿ÉÖ¤Ãâ·ÑÊÚȨÈκλñµÃ¸Ã +Èí¼þ¼°ÆäÏà¹ØÎĵµ£¨Í³³ÆÎª¡°Èí¼þ¡±£©µÄÈËÎÞÏÞÖÆµØ¾­Óª¸ÃÈí¼þ£¬°üÀ¨ÎÞÏÞÖÆµÄʹÓᢸ´ÖÆ¡¢Ð޸ġ¢ºÏ²¢¡¢ +³ö°æ·¢ÐС¢É¢²¼¡¢ÔÙÊÚȨ¡¢¼°··ÊÛÈí¼þ¼°Èí¼þ¸±±¾µÄȨÀû¡£±»ÊÚȨÈËÔÚÏíÊÜÕâЩȨÀûµÄͬʱ£¬Ðè·þ´ÓÏÂÃæ +µÄÌõ¼þ£º + +ÔÚÈí¼þºÍÈí¼þµÄËùÓи±±¾Öж¼±ØÐë°üº¬ÒÔÉϵİæÈ¨ÉùÃ÷ºÍÊÚȨÉùÃ÷¡£ + +¸ÃÈí¼þ°´±¾À´µÄÑù×ÓÌṩ£¬Ã»ÓÐÈκÎÃ÷È·»ò°µº¬µÄµ£±££¬°üÀ¨µ«²»½öÏÞÓÚ¹ØÓÚÊÔÏúÐÔ¡¢ÊʺÏÄ³Ò»ÌØ¶¨ÓÃ; +ºÍ·ÇÇÖȨµÄ±£Ö¤¡£×÷ÕߺͰæÈ¨³ÖÓÐÈËÔÚÈκÎÇé¿öϾù²»¾ÍÓÉÈí¼þ»òÈí¼þʹÓÃÒýÆðµÄÒÔºÏͬÐÎʽ¡¢ÃñÊÂÇÖȨ +»òÆäËü·½Ê½Ìá³öµÄÈκÎË÷Åâ¡¢Ë𺦻òÆäËüÔðÈθºÔð¡£ + + + + diff --git a/targets/esp8266/Makefile b/targets/esp8266/Makefile new file mode 100644 index 000000000..70a0e72ea --- /dev/null +++ b/targets/esp8266/Makefile @@ -0,0 +1,146 @@ +# 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. + +############################################################# +# 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 object file images to be generated () +# GEN_BINS - list of binaries 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 () +# +# Tabsize : 8 +# +TARGET = eagle +FLAVOR = release +#FLAVOR = debug + +#EXTRA_CCFLAGS += -u + +ifndef PDIR # { +GEN_IMAGES= eagle.app.v6.out +GEN_BINS= eagle.app.v6.bin +SPECIAL_MKTARGETS=$(APP_MKTARGETS) +SUBDIRS= user +endif # } PDIR + +LDDIR = $(SDK_PATH)/ld + +CCFLAGS += -Os +#CCFLAGS += -O0 + +TARGET_LDFLAGS = \ + -nostdlib \ + -Wl,-EL \ + --longcalls \ + --text-section-literals + +ifeq ($(FLAVOR),debug) + TARGET_LDFLAGS += -O0 +endif + +ifeq ($(FLAVOR),release) + TARGET_LDFLAGS += -Os +endif + +COMPONENTS_eagle.app.v6 = \ + user/libuser.a \ + + +LINKFLAGS_eagle.app.v6 = \ + -L$(SDK_PATH)/lib \ + -Wl,--gc-sections \ + -Wl,-Map,output.map \ + -nostdlib \ + -T$(LD_FILE) \ + -Wl,--no-check-sections \ + -u call_user_start \ + -Wl,-static \ + -Wl,--start-group \ + -lminic \ + -lgcc \ + -lhal \ + -lphy \ + -lpp \ + -lnet80211 \ + -lwpa \ + -lmain \ + -lfreertos \ + -llwip \ + -L./libs \ + -ljerrycore \ + -ljerryentry \ + -lfdlibm \ + $(DEP_LIBS_eagle.app.v6) \ + ./libs/lib_a-setjmp.o \ + -Wl,--end-group + + +DEPENDS_eagle.app.v6 = \ + $(LD_FILE) \ + $(LDDIR)/eagle.rom.addr.v6.ld \ + ./source/jerry_targetjs.h \ + ./libs/libfdlibm.a \ + ./libs/libjerrycore.a \ + ./libs/libjerryentry.a + + +############################################################# +# 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 +# + +#UNIVERSAL_TARGET_DEFINES = \ + +# Other potential configuration flags include: +# -DTXRX_TXBUF_DEBUG +# -DTXRX_RXBUF_DEBUG +# -DWLAN_CONFIG_CCX +CONFIGURATION_DEFINES = -DICACHE_FLASH + +DEFINES += \ + $(UNIVERSAL_TARGET_DEFINES) \ + $(CONFIGURATION_DEFINES) + +DDEFINES += \ + $(UNIVERSAL_TARGET_DEFINES) \ + $(CONFIGURATION_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 -I $(PDIR)source +sinclude $(SDK_PATH)/Makefile + +.PHONY: FORCE + +FORCE: diff --git a/targets/esp8266/Makefile.esp8266 b/targets/esp8266/Makefile.esp8266 new file mode 100644 index 000000000..0ac7932f5 --- /dev/null +++ b/targets/esp8266/Makefile.esp8266 @@ -0,0 +1,111 @@ +# 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` +ESP_SDK_PATH ?= $(SDK_PATH) +ESP_INC = $(ESP_SDK_PATH)/extra_include +ESP_INC2 = $(ESP_SDK_PATH)/include +ESP_LIB = $(ESP_SDK_PATH)/lib +TYPE = release +INTERM = build/obj-esp8266 +OUTPUT = build/bin/$(TYPE).esp8266 +SRCPATH = targets/esp8266/source +COPYTARGET = targets/esp8266/libs +USBDEVICE ?= /dev/ttyUSB0 +JERRYHEAP ?= 20 + +# compile flags +ESP_CFLAGS := -D__TARGET_ESP8266 -D__attr_always_inline___= +ESP_CFLAGS += -Wl,-EL -fno-inline-functions +ESP_CFLAGS += -ffunction-sections -fdata-sections +ESP_CFLAGS += -mlongcalls -mtext-section-literals -mno-serialize-volatile + +# include path +ESP_LIBS_INC :=$(CURDIR)/targets/esp8266/include +ESP_LIBS_INC :=$(ESP_LIBS_INC);$(ESP_INC2) +ESP_LIBS_INC :=$(ESP_LIBS_INC);$(ESP_INC2)/espressif +ESP_LIBS_INC :=$(ESP_LIBS_INC);$(ESP_INC2)/freertos +ESP_LIBS_INC :=$(ESP_LIBS_INC);$(ESP_INC2)/json +ESP_LIBS_INC :=$(ESP_LIBS_INC);$(ESP_INC2)/lwip +ESP_LIBS_INC :=$(ESP_LIBS_INC);$(ESP_INC2)/ssl + +# jerry related source files +JERRY_BUILD_FILES := $(SRCPATH)/jerry_extapi.cpp +JERRY_BUILD_FILES := $(JERRY_BUILD_FILES);$(SRCPATH)/jerry_run.cpp + + + +all: check-env jerry js2c mbed + + +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 \ + -DEXTERNAL_CMAKE_SYSTEM_PROCESSOR=xtensia-lx106 \ + -DEXTERNAL_CMAKE_C_COMPILER=xtensa-lx106-elf-gcc \ + -DEXTERNAL_CMAKE_CXX_COMPILER=xtensa-lx106-elf-g++ \ + -DEXTERNAL_BUILD_ENTRY_FILE="$(JERRY_BUILD_FILES)" \ + -DEXTERNAL_COMPILE_FLAGS="$(ESP_CFLAGS)" \ + -DEXTERNAL_LIBC_INTERFACE="$(ESP_INC)" \ + -DEXTERNAL_LIBS_INTERFACE="$(ESP_LIBS_INC)" \ + -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 + + +js2c: + cd targets/esp8266; ./js2c.py + + +mbed: + cd targets/esp8266; \ + make clean; \ + BOOT=new APP=1 SPI_SPEED=40 SPI_MODE=QIO SPI_SIZE_MAP=3 make; \ + BOOT=new APP=2 SPI_SPEED=40 SPI_MODE=QIO SPI_SIZE_MAP=3 make + + +check-env: +ifndef SDK_PATH + $(error SDK_PATH is undefined for ESP8266) +endif +ifndef BIN_PATH + $(error BIN_PATH is undefined for ESP8266) +endif + + +flash: + /opt/Espressif/esptool-py/esptool.py --port $(USBDEVICE) write_flash \ + 0x00000 $(SDK_PATH)/bin/"boot_v1.4(b1).bin" \ + 0x01000 $(BIN_PATH)/upgrade/user1.2048.new.3.bin \ + 0x81000 $(BIN_PATH)/upgrade/user2.2048.new.3.bin \ + 0x1FC000 $(SDK_PATH)/bin/esp_init_data_default.bin \ + 0x1FE000 $(SDK_PATH)/bin/blank.bin + + +clean: + rm -rf $(INTERM) + rm -rf $(OUTPUT) diff --git a/targets/esp8266/docs/ESP-PATCHFORJERRYSCRIPT.md b/targets/esp8266/docs/ESP-PATCHFORJERRYSCRIPT.md new file mode 100644 index 000000000..10bde7e43 --- /dev/null +++ b/targets/esp8266/docs/ESP-PATCHFORJERRYSCRIPT.md @@ -0,0 +1,75 @@ +#### Apply patch to ESP8266 SDK + +As `iram` is quite small to fit all the codes but linker tries to put it there. +To force JerryScript codes to be placed at the `irom` section, +need to change the order and tell the linker as below; + +``` +diff --git a/ld/eagle.app.v6.common.ld b/ld/eagle.app.v6.common.ld +index caf8e32..dadaceb 100644 +--- a/ld/eagle.app.v6.common.ld ++++ b/ld/eagle.app.v6.common.ld +@@ -151,6 +151,21 @@ SECTIONS + } >dram0_0_seg :dram0_0_bss_phdr + /* __stack = 0x3ffc8000; */ + ++ .irom0.text : ALIGN(4) ++ { ++ _irom0_text_start = ABSOLUTE(.); ++ *(.irom0.literal .irom.literal .irom.text.literal .irom0.text .irom.text) ++ *(.literal.* .text.*) ++ _irom0_text_end = ABSOLUTE(.); ++ ++ _jerry_text_start = ABSOLUTE(.); ++ *\libjerryentry.a:*(.text*) ++ *\libjerrycore.a:*(.text*) ++ *\libfdlibm.a:*(.text*) ++ _jerry_text_end = ABSOLUTE(.); ++ ++ } >irom0_0_seg :irom0_0_phdr ++ + .text : ALIGN(4) + { + _stext = .; +@@ -199,13 +214,6 @@ SECTIONS + _lit4_end = ABSOLUTE(.); + } >iram1_0_seg :iram1_0_phdr + +- .irom0.text : ALIGN(4) +- { +- _irom0_text_start = ABSOLUTE(.); +- *(.irom0.literal .irom.literal .irom.text.literal .irom0.text .irom.text) +- *(.literal.* .text.*) +- _irom0_text_end = ABSOLUTE(.); +- } >irom0_0_seg :irom0_0_phdr + } + + /* get ROM code address */ +diff --git a/ld/eagle.app.v6.ld b/ld/eagle.app.v6.ld +index 3e7ec1b..4a9ab5b 100644 +--- a/ld/eagle.app.v6.ld ++++ b/ld/eagle.app.v6.ld +@@ -26,7 +26,7 @@ MEMORY + dport0_0_seg : org = 0x3FF00000, len = 0x10 + dram0_0_seg : org = 0x3FFE8000, len = 0x14000 + iram1_0_seg : org = 0x40100000, len = 0x8000 +- irom0_0_seg : org = 0x40240000, len = 0x3C000 ++ irom0_0_seg : org = 0x40240000, len = 0xB0000 + } + + INCLUDE "../ld/eagle.app.v6.common.ld" +``` + +Second file is to modify `irom` size so that it can hold all the codes and data. +This can be done by giving another `SPI_SIZE_MAP`. +For now, I'll use this manual modification. + + +#### Need to get setjmp / longjmp + +Extract and copy from the SDK. + +``` +cd ~/harmony/jerryscript/targets/esp8266/libs +ar -xv $SDK_PATH/lib/libcirom.a lib_a-setjmp.o +``` diff --git a/targets/esp8266/docs/ESP-PREREQUISITES.md b/targets/esp8266/docs/ESP-PREREQUISITES.md new file mode 100644 index 000000000..7d1a915b8 --- /dev/null +++ b/targets/esp8266/docs/ESP-PREREQUISITES.md @@ -0,0 +1,150 @@ +#### Preparation + +##### Accessories + +You need, +* 3.3V power supply. You can use bread board power (+5V, +3.3V). I used LM317 like this; + * Use [LM317](http://www.ti.com/lit/ds/symlink/lm317.pdf) + * R1 = 330 Ohm, R2 = 545 Ohm (1K + 1.2K in parallel) + * 5V 2A adaptor +* USB to RS-232 Serial + RS-232 Serial to Digital or USB-to-RS232 TTL converter + +#### Toolchain + +Reference [Toolchain](https://github.com/esp8266/esp8266-wiki/wiki/Toolchain) page. + +I've slightly changed the step to use SDK from Espressif official SDK +(https://github.com/espressif/esp_iot_rtos_sdk) + +##### Toolchain: + +dependencies for x86 +``` +sudo apt-get install git autoconf build-essential gperf \ + bison flex texinfo libtool libncurses5-dev wget \ + gawk libc6-dev-i386 python-serial libexpat-dev +sudo mkdir /opt/Espressif +sudo chown $USER /opt/Espressif/ +``` + +dependencies for x64 +``` +sudo apt-get install git autoconf build-essential gperf \ + bison flex texinfo libtool libncurses5-dev wget \ + gawk libc6-dev-amd64 python-serial libexpat-dev +sudo mkdir /opt/Espressif +sudo chown $USER /opt/Espressif/ +``` + +crosstool-NG +``` +cd /opt/Espressif +git clone -b lx106-g++-1.21.0 git://github.com/jcmvbkbc/crosstool-NG.git +cd crosstool-NG +./bootstrap && ./configure --prefix=`pwd` && make && make install +./ct-ng xtensa-lx106-elf +./ct-ng build +``` +add path to environment file such as `.profile` +``` +PATH=$PWD/builds/xtensa-lx106-elf/bin:$PATH +``` + +##### Espressif SDK: use Espressif official + +``` +cd /opt/Esprissif +git clone https://github.com/espressif/ESP8266_RTOS_SDK.git ESP8266_RTOS_SDK.git +ln -s ESP8266_RTOS_SDK.git ESP8266_SDK +cd ESP8266_SDK +git checkout -b jerry a2b413ad2996450fe2f173b6afab243f6e1249aa +``` + +We use SDK 1.2.0 version which has stdlib.h and others. Latest 1.3.0 version, +as of writing this document, doesn't have it. +(If anyone knows how to use latest version, please add an issue or send a PR.) + + +set two environment variables such as in .profile +``` +export SDK_PATH=/opt/Espressif/ESP8266_SDK +export BIN_PATH=(to output folder path) +``` + +##### Xtensa libraries and headers: +``` +cd /opt/Espressif/ESP8266_SDK +wget -O lib/libhal.a https://github.com/esp8266/esp8266-wiki/raw/master/libs/libhal.a +``` + +##### ESP image tool +``` +cd /opt/Espressif +wget -O esptool_0.0.2-1_i386.deb https://github.com/esp8266/esp8266-wiki/raw/master/deb/esptool_0.0.2-1_i386.deb +sudo dpkg -i esptool_0.0.2-1_i386.deb +``` + +##### ESP upload tool +``` +cd /opt/Espressif +git clone https://github.com/themadinventor/esptool esptool-py +sudo ln -s $PWD/esptool-py/esptool.py crosstool-NG/builds/xtensa-lx106-elf/bin/esptool.py +``` + +#### Test writing with Blinky example + +##### Get the source + +found one example that works with SDK V1.2 (which is based on FreeRTOS, as of writing) + +* https://github.com/mattcallow/esp8266-sdk/tree/master/rtos_apps/01blinky + + +##### Compile + +Read `2A-ESP8266__IOT_SDK_User_Manual_EN` document in +[this](http://bbs.espressif.com/viewtopic.php?f=51&t=1024) link. + +It's configured 2048KB flash +``` +BOOT=new APP=1 SPI_SPEED=80 SPI_MODE=QIO SPI_SIZE_MAP=5 make +BOOT=new APP=2 SPI_SPEED=80 SPI_MODE=QIO SPI_SIZE_MAP=5 make +``` + +or old way... this works not sure this is ok. +``` +make BOOT=new APP=0 SPI_SPEED=80 SPI_MODE=QIO SPI_SIZE_MAP=2 +``` + +##### Flashing + +* power off ESP8266 board +* connect GPIO0 to GND, connect GPIO2 to VCC +* power on +* write + +``` +sudo /opt/Espressif/esptool-py/esptool.py \ + --port /dev/ttyUSB0 write_flash \ + 0x00000 $SDK_PATH/bin/"boot_v1.4(b1).bin" \ + 0x01000 $BIN_PATH/upgrade/user1.2048.new.5.bin \ + 0x101000 $BIN_PATH/upgrade/user2.2048.new.5.bin \ + 0x3FE000 $SDK_PATH/bin/blank.bin \ + 0x3FC000 $SDK_PATH/bin/esp_init_data_default.bin +``` +_change `/dev/ttyUSB1` to whatever you have._ + +or the old way...this works not sure this is ok. +``` +cd $BIN_PATH +sudo /opt/Espressif/esptool-py/esptool.py \ +--port /dev/ttyUSB0 write_flash \ +0x00000 eagle.flash.bin 0x40000 eagle.irom0text.bin +``` + +* power off +* disconnect GPIO0 so that it is floating +* connect GPIO2 with serial of 470 Ohm + LED and to GND +* power On + +LED should blink on and off every second diff --git a/targets/esp8266/gen_misc.sh b/targets/esp8266/gen_misc.sh new file mode 100644 index 000000000..9e8a86312 --- /dev/null +++ b/targets/esp8266/gen_misc.sh @@ -0,0 +1,165 @@ +#!/bin/bash + +echo "gen_misc.sh version 20150826" +echo "" + +if [ $SDK_PATH ]; then + echo "SDK_PATH:" + echo "$SDK_PATH" + echo "" +else + echo "ERROR: Please export SDK_PATH firstly, exit!!!" + exit +fi + +if [ $BIN_PATH ]; then + echo "BIN_PATH:" + echo "$BIN_PATH" + echo "" +else + echo "ERROR: Please export BIN_PATH firstly, exit!!!" + exit +fi + +echo "Please check SDK_PATH & BIN_PATH, enter (Y/y) to continue:" +read input + +if [[ $input != Y ]] && [[ $input != y ]]; then + exit +fi + +echo "" + +echo "Please follow below steps(1-5) to generate specific bin(s):" +echo "STEP 1: use boot_v1.2+ by default" +boot=new + +echo "boot mode: $boot" +echo "" + +echo "STEP 2: choose bin generate(0=eagle.flash.bin+eagle.irom0text.bin, 1=user1.bin, 2=user2.bin)" +echo "enter (0/1/2, default 0):" +read input + +if [ -z "$input" ]; then + if [ $boot != none ]; then + boot=none + echo "ignore boot" + fi + app=0 + echo "generate bin: eagle.flash.bin+eagle.irom0text.bin" +elif [ $input == 1 ]; then + if [ $boot == none ]; then + app=0 + echo "choose no boot before" + echo "generate bin: eagle.flash.bin+eagle.irom0text.bin" + else + app=1 + echo "generate bin: user1.bin" + fi +elif [ $input == 2 ]; then + if [ $boot == none ]; then + app=0 + echo "choose no boot before" + echo "generate bin: eagle.flash.bin+eagle.irom0text.bin" + else + app=2 + echo "generate bin: user2.bin" + fi +else + if [ $boot != none ]; then + boot=none + echo "ignore boot" + fi + app=0 + echo "generate bin: eagle.flash.bin+eagle.irom0text.bin" +fi + +echo "" + +echo "STEP 3: choose spi speed(0=20MHz, 1=26.7MHz, 2=40MHz, 3=80MHz)" +echo "enter (0/1/2/3, default 2):" +read input + +if [ -z "$input" ]; then + spi_speed=40 +elif [ $input == 0 ]; then + spi_speed=20 +elif [ $input == 1 ]; then + spi_speed=26.7 +elif [ $input == 3 ]; then + spi_speed=80 +else + spi_speed=40 +fi + +echo "spi speed: $spi_speed MHz" +echo "" + +echo "STEP 4: choose spi mode(0=QIO, 1=QOUT, 2=DIO, 3=DOUT)" +echo "enter (0/1/2/3, default 0):" +read input + +if [ -z "$input" ]; then + spi_mode=QIO +elif [ $input == 1 ]; then + spi_mode=QOUT +elif [ $input == 2 ]; then + spi_mode=DIO +elif [ $input == 3 ]; then + spi_mode=DOUT +else + spi_mode=QIO +fi + +echo "spi mode: $spi_mode" +echo "" + +echo "STEP 5: choose spi size and map" +echo " 0= 512KB( 256KB+ 256KB)" +echo " 2=1024KB( 512KB+ 512KB)" +echo " 3=2048KB( 512KB+ 512KB)" +echo " 4=4096KB( 512KB+ 512KB)" +echo " 5=2048KB(1024KB+1024KB)" +echo " 6=4096KB(1024KB+1024KB)" +echo "enter (0/2/3/4/5/6, default 0):" +read input + +if [ -z "$input" ]; then + spi_size_map=0 + echo "spi size: 512KB" + echo "spi ota map: 256KB + 256KB" +elif [ $input == 2 ]; then + spi_size_map=2 + echo "spi size: 1024KB" + echo "spi ota map: 512KB + 512KB" +elif [ $input == 3 ]; then + spi_size_map=3 + echo "spi size: 2048KB" + echo "spi ota map: 512KB + 512KB" +elif [ $input == 4 ]; then + spi_size_map=4 + echo "spi size: 4096KB" + echo "spi ota map: 512KB + 512KB" +elif [ $input == 5 ]; then + spi_size_map=5 + echo "spi size: 2048KB" + echo "spi ota map: 1024KB + 1024KB" +elif [ $input == 6 ]; then + spi_size_map=6 + echo "spi size: 4096KB" + echo "spi ota map: 1024KB + 1024KB" +else + spi_size_map=0 + echo "spi size: 512KB" + echo "spi ota map: 256KB + 256KB" +fi + +echo "" + +echo "start..." +echo "" + +make clean + +make BOOT=$boot APP=$app SPI_SPEED=$spi_speed SPI_MODE=$spi_mode SPI_SIZE_MAP=$spi_size_map diff --git a/targets/esp8266/include/esp8266_gpio.h b/targets/esp8266/include/esp8266_gpio.h new file mode 100644 index 000000000..5bee25b93 --- /dev/null +++ b/targets/esp8266/include/esp8266_gpio.h @@ -0,0 +1,151 @@ +/* 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. + */ + +/* + * Copyright (C) 2014 -2016 Espressif System + * + */ + +#ifndef __ESP82666_GPIO_H__ +#define __ESP82666_GPIO_H__ + +#define GPIO_Pin_0 (BIT(0)) /* Pin 0 selected */ +#define GPIO_Pin_1 (BIT(1)) /* Pin 1 selected */ +#define GPIO_Pin_2 (BIT(2)) /* Pin 2 selected */ +#define GPIO_Pin_3 (BIT(3)) /* Pin 3 selected */ +#define GPIO_Pin_4 (BIT(4)) /* Pin 4 selected */ +#define GPIO_Pin_5 (BIT(5)) /* Pin 5 selected */ +#define GPIO_Pin_6 (BIT(6)) /* Pin 6 selected */ +#define GPIO_Pin_7 (BIT(7)) /* Pin 7 selected */ +#define GPIO_Pin_8 (BIT(8)) /* Pin 8 selected */ +#define GPIO_Pin_9 (BIT(9)) /* Pin 9 selected */ +#define GPIO_Pin_10 (BIT(10)) /* Pin 10 selected */ +#define GPIO_Pin_11 (BIT(11)) /* Pin 11 selected */ +#define GPIO_Pin_12 (BIT(12)) /* Pin 12 selected */ +#define GPIO_Pin_13 (BIT(13)) /* Pin 13 selected */ +#define GPIO_Pin_14 (BIT(14)) /* Pin 14 selected */ +#define GPIO_Pin_15 (BIT(15)) /* Pin 15 selected */ +#define GPIO_Pin_All (0xFFFF) /* All pins selected */ + +#define GPIO_PIN_REG_0 PERIPHS_IO_MUX_GPIO0_U +#define GPIO_PIN_REG_1 PERIPHS_IO_MUX_U0TXD_U +#define GPIO_PIN_REG_2 PERIPHS_IO_MUX_GPIO2_U +#define GPIO_PIN_REG_3 PERIPHS_IO_MUX_U0RXD_U +#define GPIO_PIN_REG_4 PERIPHS_IO_MUX_GPIO4_U +#define GPIO_PIN_REG_5 PERIPHS_IO_MUX_GPIO5_U +#define GPIO_PIN_REG_6 PERIPHS_IO_MUX_SD_CLK_U +#define GPIO_PIN_REG_7 PERIPHS_IO_MUX_SD_DATA0_U +#define GPIO_PIN_REG_8 PERIPHS_IO_MUX_SD_DATA1_U +#define GPIO_PIN_REG_9 PERIPHS_IO_MUX_SD_DATA2_U +#define GPIO_PIN_REG_10 PERIPHS_IO_MUX_SD_DATA3_U +#define GPIO_PIN_REG_11 PERIPHS_IO_MUX_SD_CMD_U +#define GPIO_PIN_REG_12 PERIPHS_IO_MUX_MTDI_U +#define GPIO_PIN_REG_13 PERIPHS_IO_MUX_MTCK_U +#define GPIO_PIN_REG_14 PERIPHS_IO_MUX_MTMS_U +#define GPIO_PIN_REG_15 PERIPHS_IO_MUX_MTDO_U + +#define GPIO_PIN_REG(i) \ + (i==0) ? GPIO_PIN_REG_0: \ + (i==1) ? GPIO_PIN_REG_1: \ + (i==2) ? GPIO_PIN_REG_2: \ + (i==3) ? GPIO_PIN_REG_3: \ + (i==4) ? GPIO_PIN_REG_4: \ + (i==5) ? GPIO_PIN_REG_5: \ + (i==6) ? GPIO_PIN_REG_6: \ + (i==7) ? GPIO_PIN_REG_7: \ + (i==8) ? GPIO_PIN_REG_8: \ + (i==9) ? GPIO_PIN_REG_9: \ + (i==10)? GPIO_PIN_REG_10: \ + (i==11)? GPIO_PIN_REG_11: \ + (i==12)? GPIO_PIN_REG_12: \ + (i==13)? GPIO_PIN_REG_13: \ + (i==14)? GPIO_PIN_REG_14: \ + GPIO_PIN_REG_15 + +#define GPIO_PIN_ADDR(i) (GPIO_PIN0_ADDRESS + i*4) + +#define GPIO_ID_IS_PIN_REGISTER(reg_id) ((reg_id >= GPIO_ID_PIN0) && (reg_id <= GPIO_ID_PIN(GPIO_PIN_COUNT-1))) + +#define GPIO_REGID_TO_PINIDX(reg_id) ((reg_id) - GPIO_ID_PIN0) + +typedef enum +{ + GPIO_PIN_INTR_DISABLE = 0, + GPIO_PIN_INTR_POSEDGE = 1, + GPIO_PIN_INTR_NEGEDGE = 2, + GPIO_PIN_INTR_ANYEGDE = 3, + GPIO_PIN_INTR_LOLEVEL = 4, + GPIO_PIN_INTR_HILEVEL = 5 +} GPIO_INT_TYPE; + +typedef enum +{ + GPIO_Mode_Input = 0x0, + GPIO_Mode_Out_OD, + GPIO_Mode_Output , + GPIO_Mode_Sigma_Delta +} GPIOMode_TypeDef; + +typedef enum +{ + GPIO_PullUp_DIS = 0x0, + GPIO_PullUp_EN = 0x1 +} GPIO_Pullup_IF; + +typedef struct +{ + uint16 GPIO_Pin; + GPIOMode_TypeDef GPIO_Mode; + GPIO_Pullup_IF GPIO_Pullup; + GPIO_INT_TYPE GPIO_IntrType; +} GPIO_ConfigTypeDef; + +#define GPIO_OUTPUT_SET(gpio_no, bit_value) gpio_output_conf (bit_value<> gpio_no)&BIT0) + + +#ifdef __cplusplus +extern "C" { +#endif + + +void gpio16_output_conf (void); +void gpio16_output_set (uint8 value); +void gpio16_input_conf (void); +uint8 gpio16_input_get (void); + +void gpio_output_conf (uint32 set_mask, uint32 clear_mask, uint32 enable_mask, uint32 disable_mask); +void gpio_intr_handler_register (void *fn); +void gpio_pin_wakeup_enable (uint32 i, GPIO_INT_TYPE intr_state); +void gpio_pin_wakeup_disable (); +void gpio_pin_intr_state_set (uint32 i, GPIO_INT_TYPE intr_state); +uint32 gpio_input_get (void); + + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/targets/esp8266/include/esp8266_uart.h b/targets/esp8266/include/esp8266_uart.h new file mode 100644 index 000000000..8e8e624fc --- /dev/null +++ b/targets/esp8266/include/esp8266_uart.h @@ -0,0 +1,48 @@ +/* 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. + */ + +/* + * Copyright (C) 2014 -2016 Espressif System + * + */ + +#ifndef __ESP8266_UART_H__ +#define __ESP8266_UART_H__ + +typedef enum { + UART0 = 0x0, + UART1 = 0x1, +} UART_Port; + +typedef enum { + BIT_RATE_300 = 300, + BIT_RATE_600 = 600, + BIT_RATE_1200 = 1200, + BIT_RATE_2400 = 2400, + BIT_RATE_4800 = 4800, + BIT_RATE_9600 = 9600, + BIT_RATE_19200 = 19200, + BIT_RATE_38400 = 38400, + BIT_RATE_57600 = 57600, + BIT_RATE_74880 = 74880, + BIT_RATE_115200 = 115200, + BIT_RATE_230400 = 230400, + BIT_RATE_460800 = 460800, + BIT_RATE_921600 = 921600, + BIT_RATE_1843200 = 1843200, + BIT_RATE_3686400 = 3686400, +} UART_BautRate; + +#endif diff --git a/targets/esp8266/include/jerry_extapi.h b/targets/esp8266/include/jerry_extapi.h new file mode 100644 index 000000000..77b73dade --- /dev/null +++ b/targets/esp8266/include/jerry_extapi.h @@ -0,0 +1,49 @@ +/* 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)->v_object)) + +#define JS_VALUE_TO_NUMBER(val_p) \ + ((val_p)->type == JERRY_API_DATA_TYPE_FLOAT32 ? \ + static_cast((val_p)->v_float32) : \ + (val_p)->type == JERRY_API_DATA_TYPE_FLOAT64 ? \ + static_cast((val_p)->v_float64) : \ + static_cast((val_p)->v_uint32)) + + +#ifdef __cplusplus +extern "C" { +#endif + + +void js_register_functions (void); + + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/targets/esp8266/include/jerry_run.h b/targets/esp8266/include/jerry_run.h new file mode 100644 index 000000000..71a551b79 --- /dev/null +++ b/targets/esp8266/include/jerry_run.h @@ -0,0 +1,34 @@ +/* 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__ + +#ifdef __cplusplus +extern "C" { +#endif + + +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); + + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/targets/esp8266/include/native_esp8266.h b/targets/esp8266/include/native_esp8266.h new file mode 100644 index 000000000..dba124e39 --- /dev/null +++ b/targets/esp8266/include/native_esp8266.h @@ -0,0 +1,33 @@ +/* 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 __NATIVE_ESP8266_H__ +#define __NATIVE_ESP8266_H__ + +#ifdef __cplusplus +extern "C" { +#endif + + +void native_gpio_dir (int, int); +void native_gpio_set (int, int); +int native_gpio_get (int); + + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/targets/esp8266/include/user_config.h b/targets/esp8266/include/user_config.h new file mode 100644 index 000000000..47008ba21 --- /dev/null +++ b/targets/esp8266/include/user_config.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 __USER_CONFIG_H__ +#define __USER_CONFIG_H__ + + +/* number of stack items, x4 for bytes */ +#define JERRY_STACK_SIZE 2000 + + +#endif diff --git a/targets/esp8266/js/blink.js b/targets/esp8266/js/blink.js new file mode 100644 index 000000000..b71a626ce --- /dev/null +++ b/targets/esp8266/js/blink.js @@ -0,0 +1,15 @@ +var check = 1; + +function blink() { + var inp = gpio_get(0); + var blk = (check > 8) ? 1 - inp : inp; + gpio_set(2, blk); + check = check >= 10 ? 1 : check+1; +} + +// GPIO 0 as input +// GPIO 2 as output +gpio_dir(0, 0); +gpio_dir(2, 1); + +print("blink js OK"); diff --git a/targets/esp8266/js/main.js b/targets/esp8266/js/main.js new file mode 100644 index 000000000..0d06812aa --- /dev/null +++ b/targets/esp8266/js/main.js @@ -0,0 +1,4 @@ +function sysloop(ticknow) { + blink(); +}; +print("main js OK"); diff --git a/targets/esp8266/js2c.py b/targets/esp8266/js2c.py new file mode 100755 index 000000000..3257e6a95 --- /dev/null +++ b/targets/esp8266/js2c.py @@ -0,0 +1,144 @@ +#!/usr/bin/env python + +# 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. +# +# This file converts ./js/*.js to a C-array in source/jerry_targetjs.h file + +import sys +import glob +import os +import re + +def extractName(path): + return os.path.splitext(os.path.basename(path))[0] + +def writeLine(fo, content, indent=0): + buf = ' ' * indent + content + '\n' + fo.write(buf) + +def regroup(l, n): + return [ l[i:i+n] for i in range(0, len(l), n) ] + +def removeComments(code): + pattern = r'(\".*?\"|\'.*?\')|(/\*.*?\*/|//[^\r\n]*$)' + regex = re.compile(pattern, re.MULTILINE | re.DOTALL) + def _replacer(match): + if match.group(2) is not None: + return "" + else: + return match.group(1) + return regex.sub(_replacer, code) + +def removeWhitespaces(code): + return re.sub('\n+', '\n', re.sub('\n +', '\n', code)) + + +LICENSE = '''/* 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. + * + * This file is generated by js2c.py. Please do not modify. + */ + +''' + +HEADER = '''#ifndef JERRY_TARGETJS_H +#define JERRY_TARGETJS_H + +''' + +FOOTER = ''' +#endif + +''' + +OUT_PATH = './source/' +SRC_PATH = './js/' + +# argument processing +buildtype = 'release' +if len(sys.argv) >= 2: + buildtype = sys.argv[1] + +fout = open(OUT_PATH + 'jerry_targetjs.h', 'w') +fout.write(LICENSE); +fout.write(HEADER); + +def exportOneFile(path, name): + fout.write('const static char ' + name + '_n[] = "' + name + '";\n') + fout.write('const static char ' + name + '_s[] =\n{\n') + + fin = open(path, 'r'); + code = fin.read() + '\0' + + # minimize code when release mode + if buildtype != 'debug': + code = removeComments(code) + code = removeWhitespaces(code) + + for line in regroup(code, 10): + buf = ', '.join(map(lambda ch: format(ord(ch),"#04x"), line)) + if line[-1] != '\0': + buf += ',' + writeLine(fout, buf, 1) + writeLine(fout, '};') + writeLine(fout, 'const static int ' + name + '_l = ' + str(len(code)-1) + ';') + writeLine(fout, '') + + fin.close(); + +def exportOneName(name): + writeLine(fout, '{ ' + name + '_n, ' + name + '_s, ' + name + '_l }, \\', 1) + +files = glob.glob(SRC_PATH + '*.js') +for path in files: + name = extractName(path) + exportOneFile(path, name) + + +NATIVE_STRUCT = ''' +struct js_source_all { + const char* name; + const char* source; + const int length; +}; + +#define DECLARE_JS_CODES \\ +struct js_source_all js_codes[] = \\ +{ \\ +''' + +fout.write(NATIVE_STRUCT) +exportOneName('main') +filenames = map(extractName, files) +for name in filenames: + if name != 'main': + exportOneName(name) + +writeLine(fout, '{ NULL, NULL, 0 } \\', 1) +writeLine(fout, '};') + +fout.write(FOOTER) +fout.close() diff --git a/targets/esp8266/readme.md b/targets/esp8266/readme.md new file mode 100644 index 000000000..44c5f2c59 --- /dev/null +++ b/targets/esp8266/readme.md @@ -0,0 +1,67 @@ +### About + +Files in this folder (embedding/esp8266) are copied from +examples/project_template of esp_iot_rtos_sdk and modified for JerryScript. +You can view online from [this](https://github.com/espressif/esp_iot_rtos_sdk/tree/master/examples/project_template) page. + + +### How to build JerryScript for ESP8266 + +#### 1. SDK + +Follow [this](./docs/ESP-PREREQUISITES.md) page to setup build environment + + +#### 2. Patch ESP-SDK for JerryScript + +Follow [this](./docs/ESP-PATCHFORJERRYSCRIPT.md) page to patch for JerryScript building. +Below is a summary after SDK patch is applied. + +#### 3. Building JerryScript + +``` +cd ~/harmony/jerryscript +make -f ./targets/esp8266/Makefile.esp8266 clean +make -f ./targets/esp8266/Makefile.esp8266 +``` + +Output files should be placed at $BIN_PATH + +#### 4. Flashing for ESP8266 ESP-01 board (WiFi Module) + +Steps are for ESP8266 ESP-01(WiFi) board. Others may vary. +Refer http://www.esp8266.com/wiki/doku.php?id=esp8266-module-family page. + +##### 4.1 GPIO0 and GPIO2 + +Before flashing you need to follow the steps. + +1. Power off ESP8266 +2. Connect GPIO0 to GND and GPIO2 to VCC +3. Power on ESP8266 +4. Flash + +##### 4.2 Flashing + +``` +make -f ./targets/esp8266/Makefile.esp8266 flash +``` + +Default USB device is `/dev/ttyUSB0`. If you have different one, give with `USBDEVICE`, like; + +``` +USBDEVICE=/dev/ttyUSB1 make -f ./targets/esp8266/Makefile.esp8266 flash +``` + + +### 5. Running + +1. Power off +2. Disonnect(float) both GPIO0 and GPIO2 +3. Power on + +Sample program here works with LED and a SW with below connection. +* Connect GPIO2 to a LED > 4K resistor > GND +* Connect GPIO0 between VCC > 4K resistor and GND + +If GPIO0 is High then LED is turned on longer. If L vice versa. diff --git a/targets/esp8266/source/jerry_extapi.cpp b/targets/esp8266/source/jerry_extapi.cpp new file mode 100644 index 000000000..76af2e1db --- /dev/null +++ b/targets/esp8266/source/jerry_extapi.cpp @@ -0,0 +1,195 @@ +/* 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. + */ + +#include +#include + +#include "jerry-core/jerry.h" +#include "jerry-core/jrt/jrt.h" /* for JERRY_ERROR_MSG */ +#include "jerry_extapi.h" + +#include "native_esp8266.h" + + + +#ifndef MIN +#define MIN(A,B) ((A)<(B)?(A):(B)) +#endif + + +#define __UNSED__ __attribute__((unused)) + +#define DELCARE_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) + +//----------------------------------------------------------------------------- + +DELCARE_HANDLER(assert) { + if (args_cnt == 1 + && args_p[0].type == JERRY_API_DATA_TYPE_BOOLEAN + && args_p[0].v_bool == true) + { + printf (">> Jerry assert true\r\n"); + return true; + } + JERRY_ERROR_MSG ("Script assertion failed\n"); + exit (JERRY_STANDALONE_EXIT_CODE_FAIL); + return false; +} + + +DELCARE_HANDLER(print) { + jerry_api_length_t cc; + + if (args_cnt) + { + printf(">> print(%d) :", (int)args_cnt); + for (cc=0; cctype = JERRY_API_DATA_TYPE_FLOAT64; + ret_val_p->v_float64 = (double)value; + + return true; +} /* gpio_dir_handler */ + + +//----------------------------------------------------------------------------- +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 ("!!! 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.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 ("!!! register_native_function failed: [%s]\r\n", name); + } + + return bok; +} + + +//----------------------------------------------------------------------------- + +void js_register_functions (void) +{ + REGISTER_HANDLER(assert); + REGISTER_HANDLER(print); + REGISTER_HANDLER(gpio_dir); + REGISTER_HANDLER(gpio_set); + REGISTER_HANDLER(gpio_get); +} diff --git a/targets/esp8266/source/jerry_run.cpp b/targets/esp8266/source/jerry_run.cpp new file mode 100644 index 000000000..6ecda39f0 --- /dev/null +++ b/targets/esp8266/source/jerry_run.cpp @@ -0,0 +1,122 @@ +/* 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. + */ + +#include +#include + +#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_init (flags); + + js_register_functions (); + + if (!jerry_parse (jerry_src, source_size)) + { + printf ("Error: jerry_parse failed\r\n"); + ret_code = JERRY_COMPLETION_CODE_UNHANDLED_EXCEPTION; + } + else + { + if ((flags & JERRY_FLAG_PARSE_ONLY) == 0) + { + ret_code = jerry_run (); + } + } + + 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].v_uint32 = ticknow; + + jerry_api_value_t res; + is_ok = jerry_api_call_function (sysloop_func.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/esp8266/user/Makefile b/targets/esp8266/user/Makefile new file mode 100644 index 000000000..eadf0bb3a --- /dev/null +++ b/targets/esp8266/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 ./ -I ../include +PDIR := ../$(PDIR) +sinclude $(PDIR)Makefile + diff --git a/targets/esp8266/user/jerry_port.c b/targets/esp8266/user/jerry_port.c new file mode 100644 index 000000000..68001dd31 --- /dev/null +++ b/targets/esp8266/user/jerry_port.c @@ -0,0 +1,66 @@ +/* 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 "esp_common.h" +#include +#include + + +/** + * Provide log message to filestream implementation for the engine. + */ +int jerry_port_logmsg (FILE* stream, const char* format, ...) +{ + va_list args; + int count = 0; + va_start (args, format); + // TODO, uncomment when vfprint link is ok + //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 = 0; + va_start (args, format); + // TODO, uncomment when vprint link is ok + //count = vprintf (format, args); + va_end (args); + return count; +} + +/** + * Provide output character to console implementation for the engine. + */ +int jerry_port_putchar (int c) +{ + printf ("%c", (char)c); + return c; +} + + +void exit (int status) +{ + printf ("!!!! EXIT: %d\n", status); + while (true) + { + ; + } +} /* exit */ diff --git a/targets/esp8266/user/native_esp8266.c b/targets/esp8266/user/native_esp8266.c new file mode 100644 index 000000000..36e8a95f0 --- /dev/null +++ b/targets/esp8266/user/native_esp8266.c @@ -0,0 +1,50 @@ +/* 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. + */ + +/****************************************************************************** + * 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); +} diff --git a/targets/esp8266/user/user_gpio.c b/targets/esp8266/user/user_gpio.c new file mode 100644 index 000000000..2e5b8d942 --- /dev/null +++ b/targets/esp8266/user/user_gpio.c @@ -0,0 +1,39 @@ +/* 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. + */ + +/* + * 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); +} diff --git a/targets/esp8266/user/user_main.c b/targets/esp8266/user/user_main.c new file mode 100644 index 000000000..925c99449 --- /dev/null +++ b/targets/esp8266/user/user_main.c @@ -0,0 +1,112 @@ +/* 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. + */ + +/****************************************************************************** + * 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 "user_config.h" +#include "esp8266_uart.h" + + +//----------------------------------------------------------------------------- + +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 */ + 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); + if (retcode != 0) { + printf("js_eval failed code(%d) [%s]\r\n", retcode, js_codes[src].name); + return -2; + } + } + show_free_mem(4); + return 0; +} + + +void jerry_task(void *pvParameters) { + const portTickType xDelay = 100 / portTICK_RATE_MS; + uint32_t ticknow = 0; + + if (jerry_task_init() == 0) { + for (;;) { + vTaskDelay(xDelay); + js_loop(ticknow); + if (!ticknow) { + show_free_mem(5); + } + ticknow++; + } + } + js_exit(); +} + + +//----------------------------------------------------------------------------- + +/* + * This is entry point for user code + */ +void ICACHE_FLASH_ATTR user_init(void) +{ + const portTickType onesec = 1000 / portTICK_RATE_MS; + uart_div_modify(UART0, UART_CLK_FREQ / (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); +}