Friday, June 28, 2019

Building Espruino for ESP8266 with storage support

Espruino ESP8266 firmware with Storage support

Note:  Storage is now included, in 2v13

Prerequesites

PC or VM with Ubuntu 18.04 LTS
ESP8266 with 4 MB flash and USB port

The following code is removed from the build: Remote access via telnet, remote firmware update, the graphics and crypto library

Basic steps

Compiling Espruino for ESP8266 on Ubuntu 18.04 LTS

Reference:
https://github.com/espruino/Espruino/blob/master/README_Building.md#for-esp8266
  • development-tools
    • sudo apt-get update && apt-get upgrade
    • sudo apt-get install make unrar-free autoconf automake libtool gcc g++ gperf flex bison texinfo gawk ncurses-dev libexpat-dev python-dev python python-serial python-pip sed git unzip bash help2man wget bzip2 libtool-bin build-essential git esptool
    • sudo pip install --upgrade pip
  • esp-open-sdk
  • Espressif NONOS SDK (version 2.2.1)
  • Espruino
    •  git clone https://github.com/espruino/Espruino.git
  • Test build
    • cd Espruino
    • export BOARD=ESP8266_4MB
    • export FLASH_4MB=1
    • export ESP8266_SDK_ROOT=/<path>/ESP8266_NONOS_SDK-2.2.1
      • Replace <path>  with the folder containing the Expressiv NONOS SDK
    • export PATH=$PATH:/<path>/xtensa-lx106-elf/bin/
      • Replace <path> with the folder containing the esp-open-sdk
    • export COMPORT=/dev/ttyUSB0
      • Replace  ttyUSB0 if you're using another port
    • make clean && make $*

Additional steps

Within the Espruino folder go to boards, make a backup copy and edit the build libraries in ESP8266_4MB.py:
'build' : {
   'libraries' : [
     'NET',
     #'TELNET',
     #'GRAPHICS',
     #'CRYPTO',
     'NEOPIXEL',
     'FILESYSTEM',
     'FLASHFS'
],
This will disable TELNET (personally I never used it), GRAPHICS (OK when building a web server) and CRYPTO (that supports SHA256 only anyway), saving storage space for the now enabled FILESYSTEM and FLASHFS.

Within the Espruino folder edit /targets/esp8266/user_main.c and comment out lines 30 (to //#include <ota.h>) and 241 (to //otaInit(88);) to remove remote firmware update from the code.

Within the Espruino folder edit Makefile and activate lines 24 (to RELEASE=1). Remove the trailing space in line 507 (to libs/network/esp8266/pktbuf.c) and remove line 508 (libs/network/esp8266/ota.c) to prevent OTA from being included in the code.


To get "make flash" to work you'll have to
  • Adjust the link to esptool.py within the Expressiv NONOS SDK subfolder (delete current link (rm /<path1>/esptool/esptool.py), add new link (ln -s /usr/share/esptool/esptool.py / <<path1>/esptool/esptool.py)

  • Grand your user access to /dev/ttyUSB0:
    • Add your user to the dialout group (sudo usermod -a -G dialout $USER)
    • Eventually stop and remove modemmanager
    • Logout and login after these changes
    • If this doesn't work do it as root
  • Flash the ESP8266
    • cd Espruino
    • export BOARD=ESP8266_4MB
    • export FLASH_4MB=1
    • export ESP8266_SDK_ROOT=/<path>/ESP8266_NONOS_SDK-2.2.1
    • export PATH=$PATH:/<path>/xtensa-lx106-elf/bin/
    • export COMPORT=/dev/ttyUSB0
    • make clean && make $*
    • make flash

If flashing doesn't produce a bootable devices then it's most likely because of the use of the option "--flash_mode qio" in the esptool.py command line, some flash modules don't support this. With "--flash_mode dio" it'll produce bootable devices. Change line 20 in the file make/family/ESP8266.make to ESP_FLASH_MODE ?= 2 # 0->QIO, 2->DIO and it'll work.)


To flash the ESP8266 using Windows copy the two binaries
espruino_esp8266_user1.bin
espruino_esp8266_user2.bin 
to Windows, and use them together with
blank.bin, boot_v1.6.bin, esp_init_data_default.bin
from  https://www.espruino.com/binaries/espruino_2v03_esp8266_4mb/ for flashing.

Testing

Connect the ESP8266 to the IDE and execute a few commands
1+1;
=2

process.env
={
  VERSION: "2v03.38",
  GIT_COMMIT: "0b21c5d4",
  BOARD: "ESP8266_4MB_FS",
  FLASH: 0, RAM: 81920,
  SERIAL: "dc4f2218-6db0",
  CONSOLE: "Serial1",
  MODULES: "Flash,Storage,hea" ... ",ESP8266,neopixel",
  EXPTR: 1073643644 }

console.log(process.env.MODULES);
Flash,Storage,heatshrink,net,dgram,http,NetworkJS,Wifi,ESP8266,neopixel
=undefined

const Storage = require("Storage");
Storage.getFree();

=196608

Storage.list();
=[  ]

//a test with the wrong length
Storage.write("name", [1,2,3], 0, 5);
=true

Storage.list();
=[
  "name"
 ]

Storage.read("name");
="\1\2\3\xFF\xFF"

Storage.getFree();
=196584

//a test with the correct length
Storage.write("name", "123", 0, 3);
=true

Storage.read("name");
="123"

// cleanup after the test
Storage.erase("name");

=undefined

Storage.list();
=[  ]

Storage.getFree();
=196608

No comments:

Post a Comment