From 842c0bc53822efe1957d29087a5e56fdbb9c9b17 Mon Sep 17 00:00:00 2001 From: Bertus Kruger Date: Thu, 5 Nov 2015 15:44:51 +1300 Subject: [PATCH 01/43] Doco fix... --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d90fa9b..bc5c754 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,7 @@ registers a set of callbacks with esp-link that control sensors/actuators. This commands in esp-link can receive MQTT messages, make simple callbacks into the uC to get sensor values or change actuators, and then respond back with MQTT. The way this is architected is that the attached uC registers callbacks at start-up such that the code in the esp doesn't need to -know which exact sensors/actuators the attached uC has, it learns thta through the initial +know which exact sensors/actuators the attached uC has, it learns that through the initial callback registration. Eye Candy From a50986a76dd8cdd1c5788a85bedf53d5b569d00a Mon Sep 17 00:00:00 2001 From: Thorsten von Eicken Date: Wed, 23 Sep 2015 22:41:03 -0700 Subject: [PATCH 02/43] fix readme --- README.md | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index bc5c754..209ae7f 100644 --- a/README.md +++ b/README.md @@ -253,27 +253,23 @@ modes are supported that can be set in the web UI (and the mode is saved in flas Note that even if the UART log is always off the bootloader prints to uart0 whenever the esp8266 comes out of reset. This cannot be disabled. -Outbound TCP connections ------------------------- -The attached micro-controller can open outbound TCP connections using a simple -[serial protocol](https://gist.github.com/tve/a46c44bf1f6b42bc572e). -More info and sample code forthcoming... - -Outbound HTTP REST requests ---------------------------- +Outbound HTTP REST requests and MQTT client +------------------------------------------- The V2 versions of esp-link support the espduino SLIP protocol that supports simple outbound -HTTP REST requests. The SLIP protocol consists of commands with binary arguments sent from the +HTTP REST requests as well as an MQTT client. The SLIP protocol consists of commands with +binary arguments sent from the attached microcontroller to the esp8266, which then performs the command and responds back. The responses back use a callback address in the attached microcontroller code, i.e., the command sent by the uC contains a callback address and the response from the esp8266 starts with that callback address. This enables asynchronous communication where esp-link can notify the uC when requests complete or when other actions happen, such as wifi connectivity status changes. -Support for MQTT is forthcoming. You can find a demo sketch in a fork of the espduino library at https://github.com/tve/espduino in the [examples/demo folder](https://github.com/tve/espduino/tree/master/espduino/examples/demo). +More docs forthcoming... + Contact ------- If you find problems with esp-link, please create a github issue. If you have a question, please From 992eaaf8c55448256bce1db19eb0f3b76f82d90d Mon Sep 17 00:00:00 2001 From: Thorsten von Eicken Date: Sat, 7 Nov 2015 23:29:41 -0800 Subject: [PATCH 03/43] fix wifi scan with many results --- esp-link/cgiwifi.c | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/esp-link/cgiwifi.c b/esp-link/cgiwifi.c index 81929ea..28861e9 100644 --- a/esp-link/cgiwifi.c +++ b/esp-link/cgiwifi.c @@ -223,8 +223,30 @@ static int ICACHE_FLASH_ATTR cgiWiFiStartScan(HttpdConnData *connData) { static int ICACHE_FLASH_ATTR cgiWiFiGetScan(HttpdConnData *connData) { if (connData->conn==NULL) return HTTPD_CGI_DONE; // Connection aborted. Clean up. - char buff[2048]; - int len; + char buff[1460]; + const int chunk = 1460/64; // ssid is up to 32 chars + int len = 0; + + // handle continuation call, connData->cgiData-1 is the position in the scan results where we + // we need to continue sending from (using -1 'cause 0 means it's the first call) + if (connData->cgiData) { + int next = (int)connData->cgiData-1; + int pos = next; + while (pos < cgiWifiAps.noAps && pos < next+chunk) { + len += os_sprintf(buff+len, "{\"essid\": \"%s\", \"rssi\": %d, \"enc\": \"%d\"},\n", + cgiWifiAps.apData[pos]->ssid, cgiWifiAps.apData[pos]->rssi, cgiWifiAps.apData[pos]->enc); + } + // done or more? + if (pos == cgiWifiAps.noAps) { + len += os_sprintf(buff+len-1, "]}}\n"); + httpdSend(connData, buff, len); + return HTTPD_CGI_DONE; + } else { + connData->cgiData = (void*)(pos+1); + httpdSend(connData, buff, len); + return HTTPD_CGI_MORE; + } + } jsonHeader(connData, 200); @@ -236,15 +258,9 @@ static int ICACHE_FLASH_ATTR cgiWiFiGetScan(HttpdConnData *connData) { } len = os_sprintf(buff, "{\"result\": {\"inProgress\": \"0\", \"APs\": [\n"); - for (int pos=0; posssid, cgiWifiAps.apData[pos]->rssi, - cgiWifiAps.apData[pos]->enc, (pos==cgiWifiAps.noAps-1)?"":","); - } - len += os_sprintf(buff+len, "]}}\n"); - //os_printf("Sending %d bytes: %s\n", len, buff); + connData->cgiData = (void *)1; // start with first result next time we're called httpdSend(connData, buff, len); - return HTTPD_CGI_DONE; + return HTTPD_CGI_MORE; } int ICACHE_FLASH_ATTR cgiWiFiScan(HttpdConnData *connData) { From 45e6ee6957c01631d7730093edd2a69374b31c27 Mon Sep 17 00:00:00 2001 From: Thorsten von Eicken Date: Fri, 6 Nov 2015 17:42:15 -0800 Subject: [PATCH 04/43] fix serial bridge port accept printf --- serial/serbridge.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/serial/serbridge.c b/serial/serbridge.c index 9993cc9..c4008a6 100644 --- a/serial/serbridge.c +++ b/serial/serbridge.c @@ -398,7 +398,7 @@ serbridgeConnectCb(void *arg) int i; for (i=0; itcp.local_port ,conn, i); #endif if (i==MAX_CONN) { From 9c1177686999a02101f39404c7e0fe432a7ad478 Mon Sep 17 00:00:00 2001 From: Thorsten von Eicken Date: Fri, 6 Nov 2015 23:52:17 -0800 Subject: [PATCH 05/43] switch to single firmware size --- Makefile | 21 ++------------------- esp-link/cgiflash.c | 24 ++++++++++++++++++++++++ esp-link/config.c | 24 +++++++++++++++--------- serial/serbridge.c | 2 +- 4 files changed, 42 insertions(+), 29 deletions(-) diff --git a/Makefile b/Makefile index 3602680..696fca7 100644 --- a/Makefile +++ b/Makefile @@ -97,12 +97,13 @@ YUI_COMPRESSOR ?= yuicompressor-2.4.8.jar HTML_PATH = $(abspath ./html)/ WIFI_PATH = $(HTML_PATH)wifi/ +ESP_FLASH_MAX ?= 503808 # max bin file + ifeq ("$(FLASH_SIZE)","512KB") # Winbond 25Q40 512KB flash, typ for esp-01 thru esp-11 ESP_SPI_SIZE ?= 0 # 0->512KB (256KB+256KB) ESP_FLASH_MODE ?= 0 # 0->QIO ESP_FLASH_FREQ_DIV ?= 0 # 0->40Mhz -ESP_FLASH_MAX ?= 241664 # max bin file for 512KB flash: 236KB ET_FS ?= 4m # 4Mbit flash size in esptool flash command ET_FF ?= 40m # 40Mhz flash speed in esptool flash command ET_BLANK ?= 0x7E000 # where to flash blank.bin to erase wireless settings @@ -112,7 +113,6 @@ else ifeq ("$(FLASH_SIZE)","1MB") ESP_SPI_SIZE ?= 2 # 2->1MB (512KB+512KB) ESP_FLASH_MODE ?= 0 # 0->QIO ESP_FLASH_FREQ_DIV ?= 15 # 15->80MHz -ESP_FLASH_MAX ?= 503808 # max bin file for 1MB flash: 492KB ET_FS ?= 8m # 8Mbit flash size in esptool flash command ET_FF ?= 80m # 80Mhz flash speed in esptool flash command ET_BLANK ?= 0xFE000 # where to flash blank.bin to erase wireless settings @@ -125,8 +125,6 @@ else ifeq ("$(FLASH_SIZE)","2MB") ESP_SPI_SIZE ?= 4 # 6->4MB (1MB+1MB) or 4->4MB (512KB+512KB) ESP_FLASH_MODE ?= 0 # 0->QIO, 2->DIO ESP_FLASH_FREQ_DIV ?= 15 # 15->80Mhz -ESP_FLASH_MAX ?= 503808 # max bin file for 512KB flash partition: 492KB -#ESP_FLASH_MAX ?= 1028096 # max bin file for 1MB flash partition: 1004KB ET_FS ?= 16m # 16Mbit flash size in esptool flash command ET_FF ?= 80m # 80Mhz flash speed in esptool flash command ET_BLANK ?= 0x1FE000 # where to flash blank.bin to erase wireless settings @@ -139,8 +137,6 @@ else ESP_SPI_SIZE ?= 4 # 6->4MB (1MB+1MB) or 4->4MB (512KB+512KB) ESP_FLASH_MODE ?= 0 # 0->QIO, 2->DIO ESP_FLASH_FREQ_DIV ?= 15 # 15->80Mhz -ESP_FLASH_MAX ?= 503808 # max bin file for 512KB flash partition: 492KB -#ESP_FLASH_MAX ?= 1028096 # max bin file for 1MB flash partition: 1004KB ET_FS ?= 32m # 32Mbit flash size in esptool flash command ET_FF ?= 80m # 80Mhz flash speed in esptool flash command ET_BLANK ?= 0x3FE000 # where to flash blank.bin to erase wireless settings @@ -396,18 +392,6 @@ endif # edit the loader script to add the espfs section to the end of irom with a 4 byte alignment. # we also adjust the sizes of the segments 'cause we need more irom0 -# in the end the only thing that matters wrt size is that the whole shebang fits into the -# 236KB available (in a 512KB flash) -ifeq ("$(FLASH_SIZE)","512KB") -build/eagle.esphttpd1.v6.ld: $(SDK_LDDIR)/eagle.app.v6.new.512.app1.ld - $(Q) sed -e '/\.irom\.text/{' -e 'a . = ALIGN (4);' -e 'a *(.espfs)' -e '}' \ - -e '/^ irom0_0_seg/ s/2B000/38000/' \ - $(SDK_LDDIR)/eagle.app.v6.new.512.app1.ld >$@ -build/eagle.esphttpd2.v6.ld: $(SDK_LDDIR)/eagle.app.v6.new.512.app2.ld - $(Q) sed -e '/\.irom\.text/{' -e 'a . = ALIGN (4);' -e 'a *(.espfs)' -e '}' \ - -e '/^ irom0_0_seg/ s/2B000/38000/' \ - $(SDK_LDDIR)/eagle.app.v6.new.512.app2.ld >$@ -else build/eagle.esphttpd1.v6.ld: $(SDK_LDDIR)/eagle.app.v6.new.1024.app1.ld $(Q) sed -e '/\.irom\.text/{' -e 'a . = ALIGN (4);' -e 'a *(.espfs)' -e '}' \ -e '/^ irom0_0_seg/ s/6B000/7C000/' \ @@ -416,7 +400,6 @@ build/eagle.esphttpd2.v6.ld: $(SDK_LDDIR)/eagle.app.v6.new.1024.app2.ld $(Q) sed -e '/\.irom\.text/{' -e 'a . = ALIGN (4);' -e 'a *(.espfs)' -e '}' \ -e '/^ irom0_0_seg/ s/6B000/7C000/' \ $(SDK_LDDIR)/eagle.app.v6.new.1024.app2.ld >$@ -endif espfs/mkespfsimage/mkespfsimage: espfs/mkespfsimage/ $(Q) $(MAKE) -C espfs/mkespfsimage GZIP_COMPRESSION="$(GZIP_COMPRESSION)" diff --git a/esp-link/cgiflash.c b/esp-link/cgiflash.c index ccd6865..2758aec 100644 --- a/esp-link/cgiflash.c +++ b/esp-link/cgiflash.c @@ -16,6 +16,7 @@ Some flash handling cgi routines. Used for reading the existing flash and updati #include #include +#include "cgi.h" #include "cgiflash.h" #include "espfs.h" @@ -33,10 +34,23 @@ static char* ICACHE_FLASH_ATTR check_header(void *buf) { return NULL; } +// check whether the flash map/size we have allows for OTA upgrade +static bool canOTA(void) { + enum flash_size_map map = system_get_flash_size_map(); + return map >= FLASH_SIZE_8M_MAP_512_512; +} + +static char *flash_too_small = "Flash too small for OTA update"; + //===== Cgi to query which firmware needs to be uploaded next int ICACHE_FLASH_ATTR cgiGetFirmwareNext(HttpdConnData *connData) { if (connData->conn==NULL) return HTTPD_CGI_DONE; // Connection aborted. Clean up. + if (!canOTA()) { + errorResponse(connData, 400, flash_too_small); + return HTTPD_CGI_DONE; + } + uint8 id = system_upgrade_userbin_check(); httpdStartResponse(connData, 200); httpdHeader(connData, "Content-Type", "text/plain"); @@ -55,6 +69,11 @@ int ICACHE_FLASH_ATTR cgiGetFirmwareNext(HttpdConnData *connData) { int ICACHE_FLASH_ATTR cgiUploadFirmware(HttpdConnData *connData) { if (connData->conn==NULL) return HTTPD_CGI_DONE; // Connection aborted. Clean up. + if (!canOTA()) { + errorResponse(connData, 400, flash_too_small); + return HTTPD_CGI_DONE; + } + int offset = connData->post->received - connData->post->buffLen; if (offset == 0) { connData->cgiPrivData = NULL; @@ -131,6 +150,11 @@ static ETSTimer flash_reboot_timer; int ICACHE_FLASH_ATTR cgiRebootFirmware(HttpdConnData *connData) { if (connData->conn==NULL) return HTTPD_CGI_DONE; // Connection aborted. Clean up. + if (!canOTA()) { + errorResponse(connData, 400, flash_too_small); + return HTTPD_CGI_DONE; + } + // sanity-check that the 'next' partition actually contains something that looks like // valid firmware uint8 id = system_upgrade_userbin_check(); diff --git a/esp-link/config.c b/esp-link/config.c index cd95e76..eb70a0f 100644 --- a/esp-link/config.c +++ b/esp-link/config.c @@ -35,9 +35,15 @@ typedef union { // size of the setting sector #define FLASH_SECT (4096) -// address where to flash the settings: there are 16KB of reserved space at the end of the first -// flash partition, we use the upper 8KB (2 sectors) -#define FLASH_ADDR (FLASH_SECT + FIRMWARE_SIZE + 2*FLASH_SECT) +// address where to flash the settings: if we have >512KB flash then there are 16KB of reserved +// space at the end of the first flash partition, we use the upper 8KB (2 sectors). If we only +// have 512KB then that space is used by the SDK and we use the 8KB just before that. +static uint32_t ICACHE_FLASH_ATTR flashAddr(void) { + enum flash_size_map map = system_get_flash_size_map(); + return map >= FLASH_SIZE_8M_MAP_512_512 + ? FLASH_SECT + FIRMWARE_SIZE + 2*FLASH_SECT // bootloader + firmware + 8KB free + : FLASH_SECT + FIRMWARE_SIZE - 2*FLASH_SECT;// bootloader + firmware - 8KB (risky...) +} static int flash_pri; // primary flash sector (0 or 1, or -1 for error) @@ -56,7 +62,7 @@ bool ICACHE_FLASH_ATTR configSave(void) { os_memcpy(&ff, &flashConfig, sizeof(FlashConfig)); uint32_t seq = ff.fc.seq+1; // erase secondary - uint32_t addr = FLASH_ADDR + (1-flash_pri)*FLASH_SECT; + uint32_t addr = flashAddr() + (1-flash_pri)*FLASH_SECT; if (spi_flash_erase_sector(addr>>12) != SPI_FLASH_RESULT_OK) goto fail; // no harm done, give up // calculate CRC @@ -76,7 +82,7 @@ bool ICACHE_FLASH_ATTR configSave(void) { if (spi_flash_write(addr, (void *)&ff, sizeof(uint32_t)) != SPI_FLASH_RESULT_OK) goto fail; // most likely failed, but no harm if successful // now that we have safely written the new version, erase old primary - addr = FLASH_ADDR + flash_pri*FLASH_SECT; + addr = flashAddr() + flash_pri*FLASH_SECT; flash_pri = 1-flash_pri; if (spi_flash_erase_sector(addr>>12) != SPI_FLASH_RESULT_OK) return true; // no back-up but we're OK @@ -95,8 +101,8 @@ fail: } void ICACHE_FLASH_ATTR configWipe(void) { - spi_flash_erase_sector(FLASH_ADDR>>12); - spi_flash_erase_sector((FLASH_ADDR+FLASH_SECT)>>12); + spi_flash_erase_sector(flashAddr()>>12); + spi_flash_erase_sector((flashAddr()+FLASH_SECT)>>12); } static int ICACHE_FLASH_ATTR selectPrimary(FlashFull *fc0, FlashFull *fc1); @@ -104,9 +110,9 @@ static int ICACHE_FLASH_ATTR selectPrimary(FlashFull *fc0, FlashFull *fc1); bool ICACHE_FLASH_ATTR configRestore(void) { FlashFull ff0, ff1; // read both flash sectors - if (spi_flash_read(FLASH_ADDR, (void *)&ff0, sizeof(ff0)) != SPI_FLASH_RESULT_OK) + if (spi_flash_read(flashAddr(), (void *)&ff0, sizeof(ff0)) != SPI_FLASH_RESULT_OK) os_memset(&ff0, 0, sizeof(ff0)); // clear in case of error - if (spi_flash_read(FLASH_ADDR+FLASH_SECT, (void *)&ff1, sizeof(ff1)) != SPI_FLASH_RESULT_OK) + if (spi_flash_read(flashAddr()+FLASH_SECT, (void *)&ff1, sizeof(ff1)) != SPI_FLASH_RESULT_OK) os_memset(&ff1, 0, sizeof(ff1)); // clear in case of error // figure out which one is good flash_pri = selectPrimary(&ff0, &ff1); diff --git a/serial/serbridge.c b/serial/serbridge.c index c4008a6..0f812d9 100644 --- a/serial/serbridge.c +++ b/serial/serbridge.c @@ -398,7 +398,7 @@ serbridgeConnectCb(void *arg) int i; for (i=0; itcp.local_port ,conn, i); + os_printf("Accept port %d, conn=%p, pool slot %d\n", conn->proto.tcp->local_port, conn, i); #endif if (i==MAX_CONN) { From 8867c8e9b666003439a0c89bdd810d3a0b235852 Mon Sep 17 00:00:00 2001 From: Thorsten von Eicken Date: Sat, 7 Nov 2015 23:41:24 -0800 Subject: [PATCH 06/43] fix cgiWifi --- esp-link/cgiwifi.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/esp-link/cgiwifi.c b/esp-link/cgiwifi.c index 28861e9..821328a 100644 --- a/esp-link/cgiwifi.c +++ b/esp-link/cgiwifi.c @@ -227,18 +227,22 @@ static int ICACHE_FLASH_ATTR cgiWiFiGetScan(HttpdConnData *connData) { const int chunk = 1460/64; // ssid is up to 32 chars int len = 0; + os_printf("GET scan: cgiData=%d noAps=%d\n", (int)connData->cgiData, cgiWifiAps.noAps); + // handle continuation call, connData->cgiData-1 is the position in the scan results where we // we need to continue sending from (using -1 'cause 0 means it's the first call) if (connData->cgiData) { int next = (int)connData->cgiData-1; int pos = next; while (pos < cgiWifiAps.noAps && pos < next+chunk) { - len += os_sprintf(buff+len, "{\"essid\": \"%s\", \"rssi\": %d, \"enc\": \"%d\"},\n", - cgiWifiAps.apData[pos]->ssid, cgiWifiAps.apData[pos]->rssi, cgiWifiAps.apData[pos]->enc); + len += os_sprintf(buff+len, "{\"essid\": \"%s\", \"rssi\": %d, \"enc\": \"%d\"}%c\n", + cgiWifiAps.apData[pos]->ssid, cgiWifiAps.apData[pos]->rssi, cgiWifiAps.apData[pos]->enc, + (pos+1 == cgiWifiAps.noAps) ? ' ' : ','); + pos++; } // done or more? if (pos == cgiWifiAps.noAps) { - len += os_sprintf(buff+len-1, "]}}\n"); + len += os_sprintf(buff+len, "]}}\n"); httpdSend(connData, buff, len); return HTTPD_CGI_DONE; } else { From adb0706349049b11293010e2452bde3523b3dddc Mon Sep 17 00:00:00 2001 From: Thorsten von Eicken Date: Mon, 9 Nov 2015 17:21:13 -0800 Subject: [PATCH 07/43] added built-in AVR/optiboot programmer --- avrflash | 110 +++++++ esp-link/cgi.c | 3 +- esp-link/cgi.h | 1 + esp-link/cgioptiboot.c | 580 +++++++++++++++++++++++++++++++++++ esp-link/cgioptiboot.h | 11 + esp-link/cgipins.c | 2 +- esp-link/log.c | 25 +- esp-link/main.c | 3 + esp-link/stk500.h | 44 +++ httpd/httpd.c | 61 ++-- include/espmissingincludes.h | 1 + serial/serbridge.c | 12 +- serial/serbridge.h | 3 + serial/serled.c | 1 + serial/uart.c | 17 + serial/uart.h | 15 +- 16 files changed, 842 insertions(+), 47 deletions(-) create mode 100755 avrflash create mode 100644 esp-link/cgioptiboot.c create mode 100644 esp-link/cgioptiboot.h create mode 100644 esp-link/stk500.h diff --git a/avrflash b/avrflash new file mode 100755 index 0000000..20fe890 --- /dev/null +++ b/avrflash @@ -0,0 +1,110 @@ +#! /bin/bash +# +# Flash an AVR with optiboot using the esp-link built-in programmer +# Basically we first reset the AVR and get in sync, and then send the hex file +# +# ---------------------------------------------------------------------------- +# "THE BEER-WARE LICENSE" (Revision 42): +# Thorsten von Eicken wrote this file. As long as you retain +# this notice you can do whatever you want with this stuff. If we meet some day, +# and you think this stuff is worth it, you can buy me a beer in return. +# ---------------------------------------------------------------------------- + +show_help() { + cat </dev/null; then + echo "ERROR: Cannot find curl: it is required for this script." >&2 + exit 1 +fi + +start=`date +%s` + +# ===== Parse arguments + +verbose= + +while getopts "hvx:" opt; do + case "$opt" in + h) show_help; exit 0 ;; + v) verbose=1 ;; + x) foo="$OPTARG" ;; + '?') show_help >&2; exit 1 ;; + esac +done + +# Shift off the options and optional --. +shift "$((OPTIND-1))" + +# Get the fixed arguments +if [[ $# != 2 ]]; then + show_help >&2 + exit 1 +fi +hostname=$1 +hex=$2 + +re='[-A-Za-z0-9.]+' +if [[ ! "$hostname" =~ $re ]]; then + echo "ERROR: hostname ${hostname} is not a valid hostname or ip address" >&2 + exit 1 +fi + +if [[ ! -r "$hex" ]]; then + echo "ERROR: cannot read hex file ($hex)" >&2 + exit 1 +fi + +# ===== Get AVR in sync + +[[ -n "$verbose" ]] && echo "Resetting AVR with http://$hostname/pgm/sync" >&2 +v=; [[ -n "$verbose" ]] && v=-v +sync=`curl -m 10 $v -s -w '%{http_code}' -XPOST "http://$hostname/pgm/sync"` +if [[ $? != 0 || "$sync" != 204 ]]; then + echo "Error resetting AVR" >&2 + exit 1 +fi + +while true; do + sync=`curl -m 10 $v -s "http://$hostname/pgm/sync"` + if [[ $? != 0 ]]; then + echo "Error checking sync" >&2 + exit 1 + fi + case "$sync" in + SYNC*) + echo "AVR in $sync" >&2 + break;; + "NOT READY"*) + [[ -n "$verbose" ]] && echo " Waiting for sync..." >&2 + ;; + *) + echo "Error checking sync: $sync" >&2 + exit 1 + ;; + esac + sleep 0.1 +done + +# ===== Send HEX file + +[[ -n "$verbose" ]] && echo "Sending HEX file for programming" >&2 +sync=`curl -m 10 $v -s -g -d "@$hex" "http://$hostname/pgm/upload"` +echo $sync +if [[ $? != 0 || ! "$sync" =~ ^Success ]]; then + echo "Error programming AVR" >&2 + exit 1 +fi + +sec=$(( `date +%s` - $start )) +echo "Success, took $sec seconds" >&2 +exit 0 diff --git a/esp-link/cgi.c b/esp-link/cgi.c index 0a52538..bd2a074 100644 --- a/esp-link/cgi.c +++ b/esp-link/cgi.c @@ -17,7 +17,8 @@ Some random cgi routines. #include #include "cgi.h" -void noCacheHeaders(HttpdConnData *connData, int code) { +void ICACHE_FLASH_ATTR +noCacheHeaders(HttpdConnData *connData, int code) { httpdStartResponse(connData, code); httpdHeader(connData, "Cache-Control", "no-cache, no-store, must-revalidate"); httpdHeader(connData, "Pragma", "no-cache"); diff --git a/esp-link/cgi.h b/esp-link/cgi.h index 66757fe..2b8893c 100644 --- a/esp-link/cgi.h +++ b/esp-link/cgi.h @@ -4,6 +4,7 @@ #include #include "httpd.h" +void noCacheHeaders(HttpdConnData *connData, int code); void jsonHeader(HttpdConnData *connData, int code); void errorResponse(HttpdConnData *connData, int code, char *message); diff --git a/esp-link/cgioptiboot.c b/esp-link/cgioptiboot.c new file mode 100644 index 0000000..3396ef0 --- /dev/null +++ b/esp-link/cgioptiboot.c @@ -0,0 +1,580 @@ +// Copyright (c) 2015 by Thorsten von Eicken, see LICENSE.txt in the esp-link repo + +#include +#include +#include "cgi.h" +#include "cgioptiboot.h" +#include "config.h" +#include "uart.h" +#include "stk500.h" +#include "serbridge.h" +#include "serled.h" + +#define SYNC_TIMEOUT (2000) // to achieve sync, in milliseconds +#define PGM_TIMEOUT (20000) // when sync is achieved, in milliseconds + +#define OPTIBOOT_DBG +#undef DBG +#ifdef OPTIBOOT_DBG +#define DBG(format, ...) os_printf(format, ## __VA_ARGS__) +#else +#define DBG(format, ...) do { } while(0) +#endif + +//===== global state + +static ETSTimer optibootTimer; + +static enum { // overall programming states + stateSync = 0, // trying to get sync + stateGetSig, // reading device signature + stateGetVersLo, // reading optiboot version, low bits + stateGetVersHi, // reading optiboot version, high bits + stateProg, // programming... +} progState; +static short syncCnt; // counter & timeout for sync attempts +static short ackWait; // counter of expected ACKs +static uint16_t optibootVers; + +#define RESP_SZ 64 +static char responseBuf[RESP_SZ]; // buffer to accumulate responses from optiboot +static short responseLen = 0; // amount accumulated so far +#define ERR_MAX 128 +static char errMessage[ERR_MAX]; // error message + +#define MAX_PAGE_SZ 512 // max flash page size supported +#define MAX_SAVED 512 // max chars in saved buffer +// structure used to remember request details from one callback to the next +// allocated dynamically so we don't burn so much static RAM +static struct optibootData { + char *saved; // buffer for saved incomplete hex records + char *pageBuf; // buffer for received data to be sent to AVR + uint16_t pageLen; // number of bytes in pageBuf + uint16_t pgmSz; // size of flash page to be programmed at a time + uint16_t pgmDone; // number of bytes programmed + uint32_t address; // address to write next page to + uint32_t startTime; // time of program POST request + HttpdConnData *conn; // request doing the programming, so we can cancel it + bool eof; // got EOF record +} *optibootData; + +// forward function references +static void optibootTimerCB(void *); +static void optibootUartRecv(char *buffer, short length); +static bool processRecord(char *buf, short len); +static bool programPage(void); +static void ICACHE_FLASH_ATTR armTimer(); + +static void ICACHE_FLASH_ATTR optibootInit() { + progState = stateSync; + syncCnt = 0; + ackWait = 0; + errMessage[0] = 0; + responseLen = 0; + programmingCB = optibootUartRecv; + if (optibootData != NULL) { + if (optibootData->conn != NULL) + optibootData->conn->cgiPrivData = (void *)-1; // signal that request has been aborted + if (optibootData->pageBuf) os_free(optibootData->pageBuf); + if (optibootData->saved) os_free(optibootData->saved); + os_free(optibootData); + optibootData = NULL; + } + os_timer_disarm(&optibootTimer); + DBG("OB init\n"); +} + +//===== Cgi to reset AVR and get Optiboot into sync +int ICACHE_FLASH_ATTR cgiOptibootSync(HttpdConnData *connData) { + if (connData->conn==NULL) return HTTPD_CGI_DONE; // Connection aborted. Clean up. + + // check that we know the reset pin, else error out with that + if (flashConfig.reset_pin < 0) { + errorResponse(connData, 400, "No reset pin defined"); + + } else if (connData->requestType == HTTPD_METHOD_POST) { + // issue reset + optibootInit(); + serbridgeReset(); + makeGpio(5); + gpio_output_set(0, (1<<5), (1<<5), 0); // output 0 + + // start sync timer + os_timer_disarm(&optibootTimer); + os_timer_setfn(&optibootTimer, optibootTimerCB, NULL); + os_timer_arm(&optibootTimer, 50, 0); // fire in 50ms and don't recur + + // respond with optimistic OK + noCacheHeaders(connData, 204); + httpdEndHeaders(connData); + httpdSend(connData, "", 0); + + } else if (connData->requestType == HTTPD_METHOD_GET) { + noCacheHeaders(connData, 200); + httpdEndHeaders(connData); + if (!errMessage[0] && progState >= stateProg) { + DBG("OB got sync\n"); + char buf[32]; + os_sprintf(buf, "SYNC : Optiboot %d.%d", optibootVers>>8, optibootVers&0xff); + httpdSend(connData, buf, -1); + } else if (errMessage[0] && progState == stateSync) { + DBG("OB lost sync\n"); + char buf[RESP_SZ+64]; + os_sprintf(buf, "FAILED to SYNC: %s, got <%s>", errMessage, responseBuf); + httpdSend(connData, buf, -1); + } else { + httpdSend(connData, errMessage[0] ? errMessage : "NOT READY", -1); + } + + } else { + errorResponse(connData, 404, "Only GET and POST supported"); + } + + return HTTPD_CGI_DONE; +} + +// verify that N chars are hex characters +static bool ICACHE_FLASH_ATTR checkHex(char *buf, short len) { + while (len--) { + char c = *buf++; + if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f')) + continue; + DBG("OB non-hex\n"); + os_sprintf(errMessage, "Non hex char in POST record: '%c'/0x%02x", c, c); + return false; + } + return true; +} + +// get hex value of some hex characters +static uint32_t ICACHE_FLASH_ATTR getHexValue(char *buf, short len) { + uint32_t v = 0; + while (len--) { + v = (v<<4) | (uint32_t)(*buf & 0xf); + if (*buf > '9') v += 9; + buf++; + } + return v; +} + +//===== Cgi to write firmware to Optiboot, requires prior sync call +int ICACHE_FLASH_ATTR cgiOptibootData(HttpdConnData *connData) { + if (connData->conn==NULL) return HTTPD_CGI_DONE; // Connection aborted. Clean up. + DBG("OB pgm: state=%d PrivData=%p postLen=%d\n", progState, connData->cgiPrivData, connData->post->len); + + // check that we have sync + if (errMessage[0] || progState < stateProg) { + DBG("OB not in sync\n"); + errorResponse(connData, 400, errMessage[0] ? errMessage : "Optiboot not in sync"); + return HTTPD_CGI_DONE; + } + + // check that we don't have two concurrent programming requests going on + if (connData->cgiPrivData == (void *)-1) { + DBG("OB aborted\n"); + errorResponse(connData, 400, "Request got aborted by a concurrent sync request"); + return HTTPD_CGI_DONE; + } + + // allocate data structure to track programming + if (!optibootData) { + optibootData = os_zalloc(sizeof(struct optibootData)); + char *saved = os_zalloc(MAX_SAVED+1); // need space for string terminator + char *pageBuf = os_zalloc(MAX_PAGE_SZ+MAX_SAVED/2); + if (!optibootData || !pageBuf || !saved) { + errorResponse(connData, 400, "Out of memory"); + return HTTPD_CGI_DONE; + } + optibootData->pageBuf = pageBuf; + optibootData->saved = saved; + optibootData->startTime = system_get_time(); + optibootData->pgmSz = 128; // hard coded for 328p for now, should be query string param + DBG("OB data alloc\n"); + } + + // iterate through the data received and program the AVR one block at a time + HttpdPostData *post = connData->post; + char *saved = optibootData->saved; + while (post->buffLen > 0) { + // first fill-up the saved buffer + short saveLen = strlen(saved); + if (saveLen < MAX_SAVED) { + short cpy = MAX_SAVED-saveLen; + if (cpy > post->buffLen) cpy = post->buffLen; + os_memcpy(saved+saveLen, post->buff, cpy); + saveLen += cpy; + saved[saveLen] = 0; // string terminator + os_memmove(post->buff, post->buff+cpy, post->buffLen-cpy); + post->buffLen -= cpy; + //DBG("OB cp %d buff->saved\n", cpy); + } + + // process HEX records + while (saveLen >= 11) { // 11 is minimal record length + // skip any CR/LF + short skip = 0; + while (skip < saveLen && (saved[skip] == '\n' || saved[skip] == '\r')) + skip++; + if (skip > 0) { + // shift out cr/lf (keep terminating \0) + os_memmove(saved, saved+skip, saveLen+1-skip); + saveLen -= skip; + if (saveLen < 11) break; + DBG("OB skip %d cr/lf\n", skip); + } + + // inspect whether we have a proper record start + if (saved[0] != ':') { + DBG("OB found non-: start\n"); + os_sprintf(errMessage, "Expected start of record in POST data, got %s", saved); + errorResponse(connData, 400, errMessage); + optibootInit(); + return HTTPD_CGI_DONE; + } + + if (!checkHex(saved+1, 2)) { + errorResponse(connData, 400, errMessage); + optibootInit(); + return HTTPD_CGI_DONE; + } + uint8_t recLen = getHexValue(saved+1, 2); + //DBG("OB record %d\n", recLen); + + // process record + if (saveLen >= 11+recLen*2) { + if (!processRecord(saved, 11+recLen*2)) { + DBG("OB process err %s\n", errMessage); + errorResponse(connData, 400, errMessage); + optibootInit(); + return HTTPD_CGI_DONE; + } + short shift = 11+recLen*2; + os_memmove(saved, saved+shift, saveLen+1-shift); + saveLen -= shift; + //DBG("OB %d byte record\n", shift); + } else { + break; + } + } + } + + short code; + if (post->received < post->len) { + //DBG("OB pgm need more\n"); + return HTTPD_CGI_MORE; + } + + if (optibootData->eof) { + // tell optiboot to reboot into the sketch + uart0_write_char(STK_LEAVE_PROGMODE); + uart0_write_char(CRC_EOP); + code = 200; + // calculate some stats + float dt = ((system_get_time() - optibootData->startTime)/1000)/1000.0; // in seconds + uint16_t pgmDone = optibootData->pgmDone; + optibootInit(); + os_sprintf(errMessage, "Success. %d bytes in %d.%ds, %dB/s %d%% efficient", + pgmDone, (int)dt, (int)(dt*10)%10, (int)(pgmDone/dt), + (int)(100.0*(10.0*pgmDone/flashConfig.baud_rate)/dt)); + } else { + code = 400; + optibootInit(); + os_strcpy(errMessage, "Improperly terminated POST data"); + } + DBG("OB pgm done: %d -- %s\n", code, errMessage); + noCacheHeaders(connData, code); + httpdEndHeaders(connData); + httpdSend(connData, errMessage, -1); + errMessage[0] = 0; + return HTTPD_CGI_DONE; +} + +// verify checksum +static bool ICACHE_FLASH_ATTR verifyChecksum(char *buf, short len) { + uint8_t sum = 0; + while (len >= 2) { + sum += (uint8_t)getHexValue(buf, 2); + buf += 2; + len -= 2; + } + return sum == 0; +} + +// Process a hex record -- assumes that the records starts with ':' & hex length +static bool ICACHE_FLASH_ATTR processRecord(char *buf, short len) { + buf++; len--; // skip leading ':' + // check we have all hex chars + if (!checkHex(buf, len)) return false; + // verify checksum + if (!verifyChecksum(buf, len)) { + buf[len] = 0; + os_sprintf(errMessage, "Invalid checksum for record %s", buf); + return false; + } + // dispatch based on record type + uint8_t type = getHexValue(buf+6, 2); + switch (type) { + case 0x00: { // data + //DBG("OB REC data %ld pglen=%d\n", getHexValue(buf, 2), optibootData->pageLen); + uint32_t addr = getHexValue(buf+2, 4); + // check whether we need to program previous record(s) + if (optibootData->pageLen > 0 && + addr != ((optibootData->address+optibootData->pageLen)&0xffff)) { + //DBG("OB addr chg\n"); + if (!programPage()) return false; + } + // set address, unless we're adding to the end (programPage may have changed pageLen) + if (optibootData->pageLen == 0) { + optibootData->address = (optibootData->address & 0xffff0000) | addr; + //DBG("OB set-addr 0x%lx\n", optibootData->address); + } + // append record + uint16_t recLen = getHexValue(buf, 2); + for (uint16_t i=0; ipageBuf[optibootData->pageLen++] = getHexValue(buf+8+2*i, 2); + // program page, if we have a full page + if (optibootData->pageLen >= optibootData->pgmSz) { + //DBG("OB full\n"); + if (!programPage()) return false; + } + break; } + case 0x01: // EOF + DBG("OB EOF\n"); + // program any remaining partial page + if (optibootData->pageLen > 0) + if (!programPage()) return false; + optibootData->eof = true; + break; + case 0x04: // address + DBG("OB address 0x%lx\n", getHexValue(buf+8, 4) << 16); + // program any remaining partial page + if (optibootData->pageLen > 0) + if (!programPage()) return false; + optibootData->address = getHexValue(buf+8, 4) << 16; + break; + case 0x05: // start address + // ignore, there's no way to tell optiboot that... + break; + default: + DBG("OB bad record type\n"); + os_sprintf(errMessage, "Invalid/unknown record type: 0x%02x", type); + return false; + } + return true; +} + +// Poll UART for ACKs, max 50ms +static bool pollAck() { + char recv[16]; + uint16_t need = ackWait*2; + uint16_t got = uart0_rx_poll(recv, need, 50000); + gpio_output_set(0, (1<<5), (1<<5), 0); // output 0 + if (got < need) { + os_strcpy(errMessage, "Timeout waiting for flash page to be programmed"); + return false; + } + ackWait = 0; + if (recv[0] == STK_INSYNC && recv[1] == STK_OK) + return true; + os_sprintf(errMessage, "Did not get ACK after programming cmd: %x02x %x02x", recv[0], recv[1]); + return false; +} + +// Program a flash page +static bool ICACHE_FLASH_ATTR programPage(void) { + if (optibootData->pageLen == 0) return true; + armTimer(); // keep the timerCB out of the picture + + if (ackWait > 7) { + os_sprintf(errMessage, "Lost sync while programming\n"); + return false; + } + + uint16_t pgmLen = optibootData->pageLen; + if (pgmLen > optibootData->pgmSz) pgmLen = optibootData->pgmSz; + DBG("OB pgm %d@0x%lx ackWait=%d\n", pgmLen, optibootData->address, ackWait); + + // send address to optiboot (little endian format) + gpio_output_set((1<<5), 0, (1<<5), 0); // output 1 + ackWait++; + uart0_write_char(STK_LOAD_ADDRESS); + uint16_t addr = optibootData->address >> 1; // word address + uart0_write_char(addr & 0xff); + uart0_write_char(addr >> 8); + uart0_write_char(CRC_EOP); + armTimer(); + if (!pollAck()) { + DBG("OB pgm failed in load address\n"); + return false; + } + armTimer(); + + // send page length (big-endian format, go figure...) + gpio_output_set((1<<5), 0, (1<<5), 0); // output 1 + ackWait++; + uart0_write_char(STK_PROG_PAGE); + uart0_write_char(pgmLen>>8); + uart0_write_char(pgmLen&0xff); + uart0_write_char('F'); // we're writing flash + + // send page content + for (short i=0; ipageBuf[i]); + uart0_write_char(CRC_EOP); + + armTimer(); + bool ok = pollAck(); + armTimer(); + if (!ok) { + DBG("OB pgm failed in prog page\n"); + return false; + } + + // shift data out of buffer + os_memmove(optibootData->pageBuf, optibootData->pageBuf+pgmLen, optibootData->pageLen-pgmLen); + optibootData->pageLen -= pgmLen; + optibootData->address += pgmLen; + optibootData->pgmDone += pgmLen; + + //DBG("OB pgm OK\n"); + return true; +} + +//===== Rebooting and getting sync + +static void ICACHE_FLASH_ATTR armTimer() { + os_timer_disarm(&optibootTimer); + os_timer_arm(&optibootTimer, 100, 0); // 100ms +} + +static void ICACHE_FLASH_ATTR optibootTimerCB(void *arg) { + // see whether we've issued so many sync in a row that it's time to give up + syncCnt++; + if ((progState == stateSync && syncCnt > SYNC_TIMEOUT/50) || + syncCnt > PGM_TIMEOUT/50) { + optibootInit(); + strcpy(errMessage, "abandoned after timeout"); + return; + } + + switch (progState) { + case stateSync: // we're trying to get sync, all we do here is send a sync request + uart0_write_char(STK_GET_SYNC); + uart0_write_char(CRC_EOP); + break; + case stateProg: // we're programming and we timed-out of inaction + uart0_write_char(STK_GET_SYNC); + uart0_write_char(CRC_EOP); + ackWait++; // we now expect an ACK + break; + default: // we're trying to get some info from optiboot and it should have responded! + optibootInit(); // abort + os_sprintf(errMessage, "No response in state %d\n", progState); + return; // do not re-arm timer + } + + // we need to come back... + armTimer(); +} + +// skip in-sync responses +static short ICACHE_FLASH_ATTR skipInSync(char *buf, short length) { + while (length > 1 && buf[0] == STK_INSYNC && buf[1] == STK_OK) { + // not the most efficient, oh well... + os_memcpy(buf, buf+2, length-2); + length -= 2; + } + return length; +} + +// receive response from optiboot, we only store the last response +static void ICACHE_FLASH_ATTR optibootUartRecv(char *buf, short length) { + // append what we got to what we have accumulated + if (responseLen < RESP_SZ-1) { + short cpy = RESP_SZ-1-responseLen; + if (cpy > length) cpy = length; + os_memcpy(responseBuf+responseLen, buf, cpy); + responseLen += cpy; + responseBuf[responseLen] = 0; // string terminator + } + + // dispatch based the current state + switch (progState) { + case stateSync: // we're trying to get a sync response + // look for STK_INSYNC+STK_OK at end of buffer + if (responseLen > 0 && responseBuf[responseLen-1] == STK_INSYNC) { + // missing STK_OK after STK_INSYNC, shift stuff out and try again + responseBuf[0] = STK_INSYNC; + responseLen = 1; + } else if (responseLen > 1 && responseBuf[responseLen-2] == STK_INSYNC && + responseBuf[responseLen-1] == STK_OK) { + // got sync response, send signature request + progState++; + os_memcpy(responseBuf, responseBuf+2, responseLen-2); + responseLen -= 2; + uart0_write_char(STK_READ_SIGN); + uart0_write_char(CRC_EOP); + armTimer(); // reset timer + } else { + // nothing useful, keep at most half the buffer for error message purposes + if (responseLen > RESP_SZ/2) { + os_memcpy(responseBuf, responseBuf+responseLen-RESP_SZ/2, RESP_SZ/2); + responseLen = RESP_SZ/2; + responseBuf[responseLen] = 0; // string terminator + } + } + break; + case stateGetSig: // expecting signature + responseLen = skipInSync(responseBuf, responseLen); + if (responseLen >= 5 && responseBuf[0] == STK_INSYNC && responseBuf[4] == STK_OK) { + if (responseBuf[1] == 0x1e && responseBuf[2] == 0x95 && responseBuf[3] == 0x0f) { + // right on... ask for optiboot version + progState++; + uart0_write_char(STK_GET_PARAMETER); + uart0_write_char(0x82); + uart0_write_char(CRC_EOP); + armTimer(); // reset timer + } else { + optibootInit(); // abort + os_sprintf(errMessage, "Bad programmer signature: 0x%02x 0x%02x 0x%02x\n", + responseBuf[1], responseBuf[2], responseBuf[3]); + } + os_memcpy(responseBuf, responseBuf+5, responseLen-5); + responseLen -= 5; + } + break; + case stateGetVersLo: // expecting version + if (responseLen >= 3 && responseBuf[0] == STK_INSYNC && responseBuf[2] == STK_OK) { + optibootVers = responseBuf[1]; + progState++; + os_memcpy(responseBuf, responseBuf+3, responseLen-3); + responseLen -= 3; + uart0_write_char(STK_GET_PARAMETER); + uart0_write_char(0x81); + uart0_write_char(CRC_EOP); + armTimer(); // reset timer + } + break; + case stateGetVersHi: // expecting version + if (responseLen >= 3 && responseBuf[0] == STK_INSYNC && responseBuf[2] == STK_OK) { + optibootVers |= responseBuf[1]<<8; + progState++; + os_memcpy(responseBuf, responseBuf+3, responseLen-3); + responseLen -= 3; + armTimer(); // reset timer + ackWait = 0; + } + break; + case stateProg: // count down expected sync responses + //DBG("UART recv %d\n", length); + while (responseLen >= 2 && responseBuf[0] == STK_INSYNC && responseBuf[1] == STK_OK) { + if (ackWait > 0) ackWait--; + os_memmove(responseBuf, responseBuf+2, responseLen-2); + responseLen -= 2; + } + armTimer(); // reset timer + default: + break; + } +} + diff --git a/esp-link/cgioptiboot.h b/esp-link/cgioptiboot.h new file mode 100644 index 0000000..bc4d20c --- /dev/null +++ b/esp-link/cgioptiboot.h @@ -0,0 +1,11 @@ +// Copyright (c) 2015 by Thorsten von Eicken, see LICENSE.txt in the esp-link repo + +#ifndef OPTIBOOT_H +#define OPTIBOOT_H + +#include + +int ICACHE_FLASH_ATTR cgiOptibootSync(HttpdConnData *connData); +int ICACHE_FLASH_ATTR cgiOptibootData(HttpdConnData *connData); + +#endif diff --git a/esp-link/cgipins.c b/esp-link/cgipins.c index 80326df..da4a8d1 100644 --- a/esp-link/cgipins.c +++ b/esp-link/cgipins.c @@ -17,7 +17,7 @@ static int8_t map_asn[][5] = { { 0, -1, 2, -1, 0 }, // esp-01(AVR) { 0, 2, -1, -1, 0 }, // esp-01(ARM) { 13, 12, 14, 0, 0 }, // esp-br-rev -- for test purposes - { 3, 1, 0, 2, 1 }, // esp-link-12 + { 1, 3, 0, 2, 1 }, // esp-link-12 }; static const int num_map_names = sizeof(map_names)/sizeof(char*); static const int num_map_func = sizeof(map_func)/sizeof(char*); diff --git a/esp-link/log.c b/esp-link/log.c index 1a9a985..1425cb3 100644 --- a/esp-link/log.c +++ b/esp-link/log.c @@ -18,6 +18,8 @@ static int log_pos; static bool log_no_uart; // start out printing to uart static bool log_newline; // at start of a new line +#define uart0_write_char uart1_write_char + // called from wifi reset timer to turn UART on when we loose wifi and back off // when we connect to wifi AP. Here this is gated by the flash setting void ICACHE_FLASH_ATTR @@ -64,20 +66,21 @@ log_read(void) { static void ICACHE_FLASH_ATTR log_write_char(char c) { + // log timestamp + if (log_newline) { + char buff[16]; + int l = os_sprintf(buff, "%6d> ", (system_get_time()/1000)%1000000); + if (!log_no_uart) + for (int i=0; i ", (system_get_time()/1000)%1000000); - for (int i=0; istartTime; if (dt > 0) dt = (system_get_time() - dt) / 1000; if (conn->conn && conn->url) +#if 0 os_printf("HTTP %s %s from %s -> %d in %ums, heap=%ld\n", conn->requestType == HTTPD_METHOD_GET ? "GET" : "POST", conn->url, conn->priv->from, conn->priv->code, dt, (unsigned long)system_get_free_heap_size()); +#else + os_printf("HTTP %s %s: %d, %ums, h=%ld\n", + conn->requestType == HTTPD_METHOD_GET ? "GET" : "POST", conn->url, + conn->priv->code, dt, (unsigned long)system_get_free_heap_size()); +#endif #endif conn->conn = NULL; // don't try to send anything, the SDK crashes... @@ -285,7 +291,8 @@ static void ICACHE_FLASH_ATTR xmitSendBuff(HttpdConnData *conn) { sint8 status = espconn_sent(conn->conn, (uint8_t*)conn->priv->sendBuff, conn->priv->sendBuffLen); if (status != 0) { #ifdef HTTPD_DBG - os_printf("%sERROR! espconn_sent returned %d\n", connStr, status); + os_printf("%sERROR! espconn_sent returned %d, trying to send %d to %s\n", + connStr, status, conn->priv->sendBuffLen, conn->url); #endif } conn->priv->sendBuffLen = 0; @@ -341,32 +348,35 @@ static void ICACHE_FLASH_ATTR httpdProcessRequest(HttpdConnData *conn) { //See if we can find a CGI that's happy to handle the request. while (1) { //Look up URL in the built-in URL table. - while (builtInUrls[i].url != NULL) { - int match = 0; - //See if there's a literal match - if (os_strcmp(builtInUrls[i].url, conn->url) == 0) match = 1; - //See if there's a wildcard match - if (builtInUrls[i].url[os_strlen(builtInUrls[i].url) - 1] == '*' && - os_strncmp(builtInUrls[i].url, conn->url, os_strlen(builtInUrls[i].url) - 1) == 0) match = 1; - if (match) { - //os_printf("Is url index %d\n", i); - conn->cgiData = NULL; - conn->cgi = builtInUrls[i].cgiCb; - conn->cgiArg = builtInUrls[i].cgiArg; - break; + if (conn->cgi == NULL) { + while (builtInUrls[i].url != NULL) { + int match = 0; + //See if there's a literal match + if (os_strcmp(builtInUrls[i].url, conn->url) == 0) match = 1; + //See if there's a wildcard match + if (builtInUrls[i].url[os_strlen(builtInUrls[i].url) - 1] == '*' && + os_strncmp(builtInUrls[i].url, conn->url, os_strlen(builtInUrls[i].url) - 1) == 0) match = 1; + if (match) { + //os_printf("Is url index %d\n", i); + conn->cgiData = NULL; + conn->cgi = builtInUrls[i].cgiCb; + conn->cgiArg = builtInUrls[i].cgiArg; + break; + } + i++; } - i++; - } - if (builtInUrls[i].url == NULL) { - //Drat, we're at the end of the URL table. This usually shouldn't happen. Well, just - //generate a built-in 404 to handle this. + if (builtInUrls[i].url == NULL) { + //Drat, we're at the end of the URL table. This usually shouldn't happen. Well, just + //generate a built-in 404 to handle this. #ifdef HTTPD_DBG - os_printf("%s%s not found. 404!\n", connStr, conn->url); + os_printf("%s%s not found. 404!\n", connStr, conn->url); #endif - httpdSend(conn, httpNotFoundHeader, -1); - xmitSendBuff(conn); - conn->cgi = NULL; //mark for destruction - return; + httpdSend(conn, httpNotFoundHeader, -1); + xmitSendBuff(conn); + conn->cgi = NULL; //mark for destruction. + if (conn->post) conn->post->len = 0; // skip any remaining receives + return; + } } //Okay, we have a CGI function that matches the URL. See if it wants to handle the @@ -380,7 +390,8 @@ static void ICACHE_FLASH_ATTR httpdProcessRequest(HttpdConnData *conn) { else if (r == HTTPD_CGI_DONE) { //Yep, it's happy to do so and already is done sending data. xmitSendBuff(conn); - conn->cgi = NULL; //mark conn for destruction + conn->cgi = NULL; //mark for destruction. + if (conn->post) conn->post->len = 0; // skip any remaining receives return; } else if (r == HTTPD_CGI_NOTFOUND || r == HTTPD_CGI_AUTHENTICATED) { diff --git a/include/espmissingincludes.h b/include/espmissingincludes.h index f421b6e..bf4c9d5 100644 --- a/include/espmissingincludes.h +++ b/include/espmissingincludes.h @@ -20,6 +20,7 @@ void ets_isr_unmask(unsigned intr); int ets_memcmp(const void *s1, const void *s2, size_t n); void *ets_memcpy(void *dest, const void *src, size_t n); +void *ets_memmove(void *dest, const void *src, size_t n); void *ets_memset(void *s, int c, size_t n); int ets_sprintf(char *str, const char *format, ...) __attribute__ ((format (printf, 2, 3))); int ets_str2macaddr(void *, void *); diff --git a/serial/serbridge.c b/serial/serbridge.c index 0f812d9..da8835a 100644 --- a/serial/serbridge.c +++ b/serial/serbridge.c @@ -20,6 +20,8 @@ static int8_t mcu_reset_pin, mcu_isp_pin; extern uint8_t slip_disabled; // disable slip to allow flashing of attached MCU +void (*programmingCB)(char *buffer, short length) = NULL; + // Connection pool serbridgeConnData connData[MAX_CONN]; @@ -97,7 +99,7 @@ telnetUnwrap(uint8_t *inBuf, int len, uint8_t state) os_delay_us(100L); } #ifdef SERBR_DBG - else os_printf("MCU reset: no pin\n"); + else { os_printf("MCU reset: no pin\n"); } #endif break; case DTR_OFF: @@ -115,7 +117,7 @@ telnetUnwrap(uint8_t *inBuf, int len, uint8_t state) os_delay_us(100L); } #ifdef SERBR_DBG - else os_printf("MCU isp: no pin\n"); + else { os_printf("MCU isp: no pin\n"); } #endif slip_disabled++; break; @@ -147,7 +149,7 @@ serbridgeReset() GPIO_OUTPUT_SET(mcu_reset_pin, 1); } #ifdef SERBR_DBG - else os_printf("MCU reset: no pin\n"); + else { os_printf("MCU reset: no pin\n"); } #endif } @@ -346,7 +348,9 @@ console_process(char *buf, short len) void ICACHE_FLASH_ATTR serbridgeUartCb(char *buf, short length) { - if (!flashConfig.slip_enable || slip_disabled > 0) { + if (programmingCB) { + programmingCB(buf, length); + } else if (!flashConfig.slip_enable || slip_disabled > 0) { //os_printf("SLIP: disabled got %d\n", length); console_process(buf, length); } else { diff --git a/serial/serbridge.h b/serial/serbridge.h index c389961..aad593e 100644 --- a/serial/serbridge.h +++ b/serial/serbridge.h @@ -36,4 +36,7 @@ void ICACHE_FLASH_ATTR serbridgeInitPins(void); void ICACHE_FLASH_ATTR serbridgeUartCb(char *buf, short len); void ICACHE_FLASH_ATTR serbridgeReset(); +// callback when receiving UART chars when in programming mode +extern void (*programmingCB)(char *buffer, short length); + #endif /* __SER_BRIDGE_H__ */ diff --git a/serial/serled.c b/serial/serled.c index 9a7a66f..6780eb6 100644 --- a/serial/serled.c +++ b/serial/serled.c @@ -29,6 +29,7 @@ void ICACHE_FLASH_ATTR serledFlash(int duration) { } void ICACHE_FLASH_ATTR serledInit(void) { + return; int8_t pin = flashConfig.ser_led_pin; if (pin >= 0) { makeGpio(pin); diff --git a/serial/uart.c b/serial/uart.c index 9b517c4..ec356d0 100644 --- a/serial/uart.c +++ b/serial/uart.c @@ -242,6 +242,23 @@ uart_recvTask(os_event_t *events) ETS_UART_INTR_ENABLE(); } +// Turn UART interrupts off and poll for nchars or until timeout hits +uint16_t ICACHE_FLASH_ATTR +uart0_rx_poll(char *buff, uint16_t nchars, uint32_t timeout_us) { + ETS_UART_INTR_DISABLE(); + uint16_t got = 0; + uint32_t start = system_get_time(); // time in us + while (system_get_time()-start < timeout_us) { + while (READ_PERI_REG(UART_STATUS(UART0)) & (UART_RXFIFO_CNT << UART_RXFIFO_CNT_S)) { + buff[got++] = READ_PERI_REG(UART_FIFO(UART0)) & 0xFF; + if (got == nchars) goto done; + } + } +done: + ETS_UART_INTR_ENABLE(); + return got; +} + void ICACHE_FLASH_ATTR uart0_baud(int rate) { os_printf("UART %d baud\n", rate); diff --git a/serial/uart.h b/serial/uart.h index 3553155..24fb2df 100644 --- a/serial/uart.h +++ b/serial/uart.h @@ -8,19 +8,24 @@ typedef void (*UartRecv_cb)(char *buf, short len); // Initialize UARTs to the provided baud rates (115200 recommended). This also makes the os_printf // calls use uart1 for output (for debugging purposes) -void ICACHE_FLASH_ATTR uart_init(UartBautRate uart0_br, UartBautRate uart1_br); +void uart_init(UartBautRate uart0_br, UartBautRate uart1_br); // Transmit a buffer of characters on UART0 -void ICACHE_FLASH_ATTR uart0_tx_buffer(char *buf, uint16 len); +void uart0_tx_buffer(char *buf, uint16 len); -void ICACHE_FLASH_ATTR uart0_write_char(char c); +void uart0_write_char(char c); STATUS uart_tx_one_char(uint8 uart, uint8 c); +void uart1_write_char(char c); + // Add a receive callback function, this is called on the uart receive task each time a chunk // of bytes are received. A small number of callbacks can be added and they are all called // with all new characters. -void ICACHE_FLASH_ATTR uart_add_recv_cb(UartRecv_cb cb); +void uart_add_recv_cb(UartRecv_cb cb); + +// Turn UART interrupts off and poll for nchars or until timeout hits +uint16_t uart0_rx_poll(char *buff, uint16_t nchars, uint32_t timeout_us); -void ICACHE_FLASH_ATTR uart0_baud(int rate); +void uart0_baud(int rate); #endif /* __UART_H__ */ From ca2de1a4375c90824a05aa6c82297ed7b1a7e199 Mon Sep 17 00:00:00 2001 From: Thorsten von Eicken Date: Mon, 9 Nov 2015 18:04:37 -0800 Subject: [PATCH 08/43] optiboot fixes --- esp-link/cgioptiboot.c | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/esp-link/cgioptiboot.c b/esp-link/cgioptiboot.c index 3396ef0..b6d953d 100644 --- a/esp-link/cgioptiboot.c +++ b/esp-link/cgioptiboot.c @@ -84,6 +84,29 @@ static void ICACHE_FLASH_ATTR optibootInit() { DBG("OB init\n"); } +void ICACHE_FLASH_ATTR appendPretty(char *buf, char *raw, int max) { + int off = strlen(buf); + int i = 0; + while (off < max-5) { + unsigned char c = raw[i++]; + if (c >= ' ' && c <= '~') { + buf[off++] = c; + } else if (c == '\n') { + buf[off++] = '\\'; + buf[off++] = 'n'; + } else if (c == '\r') { + buf[off++] = '\\'; + buf[off++] = 'r'; + } else { + buf[off++] = '\\'; + buf[off++] = 'x'; + buf[off++] = '0'+(c>>4)+((c>>4)>9?7:0); + buf[off++] = '0'+(c&0xff)+((c&0xff)>9?7:0); + } + } + buf[off] = 0; +} + //===== Cgi to reset AVR and get Optiboot into sync int ICACHE_FLASH_ATTR cgiOptibootSync(HttpdConnData *connData) { if (connData->conn==NULL) return HTTPD_CGI_DONE; // Connection aborted. Clean up. @@ -113,14 +136,15 @@ int ICACHE_FLASH_ATTR cgiOptibootSync(HttpdConnData *connData) { noCacheHeaders(connData, 200); httpdEndHeaders(connData); if (!errMessage[0] && progState >= stateProg) { + char buf[64]; DBG("OB got sync\n"); - char buf[32]; os_sprintf(buf, "SYNC : Optiboot %d.%d", optibootVers>>8, optibootVers&0xff); httpdSend(connData, buf, -1); } else if (errMessage[0] && progState == stateSync) { - DBG("OB lost sync\n"); - char buf[RESP_SZ+64]; - os_sprintf(buf, "FAILED to SYNC: %s, got <%s>", errMessage, responseBuf); + DBG("OB cannot sync\n"); + char buf[512]; + os_sprintf(buf, "FAILED to SYNC: %s, got:\r\n", errMessage); + appendPretty(buf, responseBuf, 512); httpdSend(connData, buf, -1); } else { httpdSend(connData, errMessage[0] ? errMessage : "NOT READY", -1); @@ -164,7 +188,7 @@ int ICACHE_FLASH_ATTR cgiOptibootData(HttpdConnData *connData) { // check that we have sync if (errMessage[0] || progState < stateProg) { - DBG("OB not in sync\n"); + DBG("OB not in sync, state=%d, err=%s\n", progState, errMessage); errorResponse(connData, 400, errMessage[0] ? errMessage : "Optiboot not in sync"); return HTTPD_CGI_DONE; } @@ -452,6 +476,7 @@ static void ICACHE_FLASH_ATTR optibootTimerCB(void *arg) { syncCnt++; if ((progState == stateSync && syncCnt > SYNC_TIMEOUT/50) || syncCnt > PGM_TIMEOUT/50) { + DBG("OB abandoned after timeout, state=%d syncCnt=%d\n", progState, syncCnt); optibootInit(); strcpy(errMessage, "abandoned after timeout"); return; @@ -470,6 +495,7 @@ static void ICACHE_FLASH_ATTR optibootTimerCB(void *arg) { default: // we're trying to get some info from optiboot and it should have responded! optibootInit(); // abort os_sprintf(errMessage, "No response in state %d\n", progState); + DBG("OB %s\n", errMessage); return; // do not re-arm timer } From 4975f05c3e8744706a9aa820cb93badb8fb51881 Mon Sep 17 00:00:00 2001 From: Thorsten von Eicken Date: Mon, 9 Nov 2015 18:08:48 -0800 Subject: [PATCH 09/43] optiboot: allow for 9600 baud --- esp-link/cgioptiboot.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/esp-link/cgioptiboot.c b/esp-link/cgioptiboot.c index b6d953d..dfeb360 100644 --- a/esp-link/cgioptiboot.c +++ b/esp-link/cgioptiboot.c @@ -468,7 +468,8 @@ static bool ICACHE_FLASH_ATTR programPage(void) { static void ICACHE_FLASH_ATTR armTimer() { os_timer_disarm(&optibootTimer); - os_timer_arm(&optibootTimer, 100, 0); // 100ms + // time-out every 50ms, except when programming to allow for 9600baud (133ms for 128 bytes) + os_timer_arm(&optibootTimer, progState==stateProg ? 200 : 50, 0); } static void ICACHE_FLASH_ATTR optibootTimerCB(void *arg) { From b94cf2e62007a88f8cd630b5ba8760813977f530 Mon Sep 17 00:00:00 2001 From: Thorsten von Eicken Date: Mon, 9 Nov 2015 21:15:18 -0800 Subject: [PATCH 10/43] updated makefile and readme --- Makefile | 4 +- README.md | 136 ++++++++++++++++++++++++++++++++++++++++-------------- 2 files changed, 103 insertions(+), 37 deletions(-) diff --git a/Makefile b/Makefile index 696fca7..10f3beb 100644 --- a/Makefile +++ b/Makefile @@ -150,7 +150,7 @@ endif # on the release tag, make release, upload esp-link.tgz into the release files #VERSION ?= "esp-link custom version" DATE := $(shell date '+%F %T') -BRANCH := $(shell if git diff --quiet HEAD; then git describe --tags; \ +BRANCH ?= $(shell if git diff --quiet HEAD; then git describe --tags; \ else git symbolic-ref --short HEAD; fi) SHA := $(shell if git diff --quiet HEAD; then git rev-parse --short HEAD | cut -d"/" -f 3; \ else echo "development"; fi) @@ -409,7 +409,7 @@ release: all $(Q) egrep -a 'esp-link [a-z0-9.]+ - 201' $(FW_BASE)/user1.bin | cut -b 1-80 $(Q) egrep -a 'esp-link [a-z0-9.]+ - 201' $(FW_BASE)/user2.bin | cut -b 1-80 $(Q) cp $(FW_BASE)/user1.bin $(FW_BASE)/user2.bin $(SDK_BASE)/bin/blank.bin \ - "$(SDK_BASE)/bin/boot_v1.4(b1).bin" wiflash release/esp-link-$(BRANCH) + "$(SDK_BASE)/bin/boot_v1.4(b1).bin" wiflash avrflash release/esp-link-$(BRANCH) $(Q) tar zcf esp-link-$(BRANCH).tgz -C release esp-link-$(BRANCH) $(Q) echo "Release file: esp-link-$(BRANCH).tgz" $(Q) rm -rf release diff --git a/README.md b/README.md index 209ae7f..7c96b3f 100644 --- a/README.md +++ b/README.md @@ -4,8 +4,9 @@ ESP-LINK This firmware connects an attached micro-controller to the internet using a ESP8266 Wifi module. It implements a number of features: - transparent bridge between Wifi and serial, useful for debugging or inputting into a uC -- flash-programming attached Arduino/AVR microcontrollers as well as LPC800-series and other - ARM microcontrollers via Wifi +- flash-programming attached Arduino/AVR microcontrollers, esp8266 modules, as well as + LPC800-series and other ARM microcontrollers via Wifi +- built-in stk500v1 programmer for AVR uC's with optiboot: program using HTTP upload of hex file - outbound TCP (and thus HTTP) connections from the attached micro-controller to the internet - outbound REST HTTP requests from the attached micro-controller to the internet, protocol based on espduino and compatible with [tuanpmt/espduino](https://github.com/tuanpmt/espduino) @@ -17,11 +18,16 @@ Many thanks to https://github.com/brunnels for contributions around the espduino ###[Releases](https://github.com/jeelabs/esp-link/releases) -- [V2.0.beta2](https://github.com/jeelabs/esp-link/releases/tag/v2.0.beta2) has REST support but - requires a 1MByte or 4MByte ESP8266 flash, e.g. esp-12 or wroom-02 -- [V1.0.1](https://github.com/jeelabs/esp-link/releases/tag/v1.0.1) is _stable_ +- [V2.1.beta1](https://github.com/jeelabs/esp-link/releases/tag/v2.1.beta1) has the new built-in + stk500v1 programmer and works on all modules (esp-01 through esp-12). This is still beta-ware! +- [V2.0.rc1](https://github.com/jeelabs/esp-link/releases/tag/v2.0.rc1) has REST support but + requires a 1MByte or 4MByte ESP8266 flash, e.g. esp-12 or wroom-02. Despite being labeled + as release candidate this is a pretty stable release. +- [V1.0.4](https://github.com/jeelabs/esp-link/releases/tag/v1.0.4) is _stable_ and has the web server, transparent bridge, flash-programming support, but lacks - the REST and upcoming MQTT support. V1 works with 512KB flash, e.g. esp-1, esp-3, ... + the REST and upcoming MQTT support. V1 works with 512KB flash, e.g. esp-01, esp-03, ... + Unless you've been using V1 and want to stay on it, the V1 series is really obsolete and + I recommend trying the latest V2 at this point. For quick support and questions: [![Chat at https://gitter.im/jeelabs/esp-link](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/jeelabs/esp-link?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) @@ -60,47 +66,64 @@ attached microcontroller, and the pin assignments card: Hardware info ------------- -This firmware is designed for esp8266 modules which have most ESP I/O pins available and -at least 1MB flash. (The V1 firmware supports modules with 512KB flash). -The default connections are: +This firmware is designed for any esp8266 module. +The recommended connections for an esp-01 module are: +- URXD: connect to TX of microcontroller +- UTXD: connect to RX of microcontroller +- GPIO0: connect to RESET of microcontroller +- GPIO2: optionally connect green LED to 3.3V (indicates wifi status) + +The recommended connections for an esp-12 module are: - URXD: connect to TX of microcontroller - UTXD: connect to RX of microcontroller - GPIO12: connect to RESET of microcontroller -- GPIO13: connect to ISP of LPC/ARM microcontroller (not used with Arduino/AVR) +- GPIO13: connect to ISP of LPC/ARM microcontroller or to GPIO0 of esp8266 being programmed + (not used with Arduino/AVR) - GPIO0: optionally connect green "conn" LED to 3.3V (indicates wifi status) - GPIO2: optionally connect yellow "ser" LED to 3.3V (indicates serial activity) -If you are using an FTDI connector, GPIO12 goes to DTR and GPIO13 goes to CTS. +If your application has problems with the boot message that is output at ~74600 baud by the ROM +at boot time you can connect an esp-12 module as follows and choose the "swap_uart" pin assignment +in the esp-link web interface: +- GPIO13: connect to TX of microcontroller +- GPIO15: connect to RX of microcontroller +- GPIO1/UTXD: connect to RESET of microcontroller +- GPIO3/URXD: connect to ISP of LPC/ARM microcontroller or to GPIO0 of esp8266 being programmed + (not used with Arduino/AVR) +- GPIO0: optionally connect green "conn" LED to 3.3V (indicates wifi status) +- GPIO2: optionally connect yellow "ser" LED to 3.3V (indicates serial activity) -If you are using an esp-12 module, you can avoid the initial boot message from the esp8266 -bootloader by using the swap-pins option. This swaps the esp8266 TX/RX to gpio15/gpio13 respectively. +If you are using an FTDI connector, GPIO12 goes to DTR and GPIO13 goes to CTS (or vice-versa, I've +seen both used, sigh). The GPIO pin assignments can be changed dynamically in the web UI and are saved in flash. Initial flashing ---------------- -(This is not necessary if you receive one of the jn-esp or esp-bridge modules from the author!) -If you want to simply flash the provided firmware binary, you can download the latest +If you want to simply flash a pre-built firmware binary, you can download the latest [release](https://github.com/jeelabs/esp-link/releases) and use your favorite ESP8266 flashing tool to flash the bootloader, the firmware, and blank settings. Detailed instructions are provided in the release notes. -Note that the firmware assumes a 512KB flash chip, which most of the esp-01 thru esp-11 -modules appear to have. A larger flash chip should work but has not been tested. +_Important_: the firmware adapts automatically to the size of the flash chip using information +stored in the boot sector (address 0). This is the standard way that the esp8266 SDK detects +the flash size. What this means is that you need to set this properly when you flash the bootloader. +If you use esptool.py you can do it using the -ff and -fs options. Wifi configuration overview ------------------ -For proper operation the end state the esp-link needs to arrive at is to have it +For proper operation the end state that esp-link needs to arrive at is to have it join your pre-existing wifi network as a pure station. -However, in order to get there the esp-link will start out as an access point and you'll have +However, in order to get there esp-link will start out as an access point and you'll have to join its network to configure it. The short version is: - 1. the esp-link creates a wifi access point with an SSID of the form `ESP_012ABC` - 2. you join your laptop or phone to the esp-link's network as a station and you configure - the esp-link wifi with your network info by pointing your browser at http://192.168.4.1/ - 3. the esp-link starts to connect to your network while continuing to also be an access point + 1. esp-link creates a wifi access point with an SSID of the form `ESP_012ABC` (some modules + use a different SSID form, such as `ai-thinker-012ABC`) + 2. you join your laptop or phone to esp-link's network as a station and you configure + esp-link wifi with your network info by pointing your browser at http://192.168.4.1/ + 3. esp-link starts to connect to your network while continuing to also be an access point ("AP+STA"), the esp-link may show up with a `esp-link.local` hostname (depends on your DHCP/DNS config) - 4. the esp-link succeeds in connecting and shuts down its own access point after 15 seconds, + 4. esp-link succeeds in connecting and shuts down its own access point after 15 seconds, you reconnect your laptop/phone to your normal network and access esp-link via its hostname or IP address @@ -156,7 +179,13 @@ Troubleshooting --------------- - verify that you have sufficient power, borderline power can cause the esp module to seemingly function until it tries to transmit and the power rail collapses -- check the "conn" LED to see which mode esp-link is in (see LED info above) +- if you just cannot flash your esp8266 module (some people call it the zombie mode) make sure you + have gpio0 and gpio15 pulled to gnd with a 1K resistor, gpio2 tied to 3.3V with 1K resistor, and + RX/TX connected without anything in series. If you need to level shift the signal going into the + esp8266's RX use a 1K resistor. Use 115200 baud in the flasher. + (For a permanent set-up I would use higher resistor values but + when nothing seems to work these are the ones I try.) +- if the flashing succeeded, check the "conn" LED to see which mode esp-link is in (see LED info above) - reset or power-cycle the esp-link to force it to become an access-point if it can't connect to your network within 15-20 seconds - if the LED says that esp-link is on your network but you can't get to it, make sure your @@ -188,7 +217,15 @@ A few notes from others (I can't fully verify these): - Make sure the paths at the beginning of the makefile are correct - Make sure `esp-open-sdk/xtensa-lx106-elf/bin` is in the PATH set in the Makefile -Flashing the firmware +It is possible to build esp-link on Windows, but it requires a gaggle of software to be installed: +- Install the unofficial sdk, mingw, SourceTree (gui git client), python 2.7, git cli, Java +- Use SourceTree to checkout under C:\espressif or wherever you installed the unofficial sdk, + (see this thread for the unofficial sdk http://www.esp8266.com/viewtopic.php?t=820) +- Create a symbolic link under c:/espressif for the git bin directory under program files and + the java bin directory under program files. +- ... + +Updating the firmware over-the-air --------------------- This firmware supports over-the-air (OTA) flashing, so you do not have to deal with serial flashing again after the initial one! The recommended way to flash is to use `make wiflash` @@ -198,10 +235,11 @@ If you are downloading firmware binaries use `./wiflash`. You can easily do that using something like `ESP_HOSTNAME=192.168.1.5 make wiflash`. The flashing, restart, and re-associating with your wireless network takes about 15 seconds -and is fully automatic. The 512KB flash are divided into two 236KB partitions allowing for new +and is fully automatic. The first 1MB of flash are divided into two 512KB partitions allowing for new code to be uploaded into one partition while running from the other. This is the official OTA upgrade method supported by the SDK, except that the firmware is POSTed to the module -using curl as opposed to having the module download it from a cloud server. +using curl as opposed to having the module download it from a cloud server. On a module with +512KB flash there is only space for one partition and thus no way to do an OTA update. If you are downloading the binary versions of the firmware (links forthcoming) you need to have both `user1.bin` and `user2.bin` handy and run `wiflash.sh user1.bin user2.bin`. @@ -220,12 +258,37 @@ Serial bridge and connections to Arduino, AVR, ARM, LPC microcontrollers In order to connect through the esp-link to a microcontroller use port 23. For example, on linux you can use `nc esp-hostname 23` or `telnet esp-hostname 23`. -You can reprogram an Arduino / AVR microcontroller by pointing avrdude at port 23. Instead of -specifying a serial port of the form /dev/ttyUSB0 use `net:esp-link:23` with avrdude's -P option -(where `esp-link` is either the hostname of your esp-link or its IP address). -The esp-link detects that avrdude starts its connection with a flash synchronization sequence +Note that multiple connections to port 23 and 2323 can be made simultaneously. Esp-link will +intermix characters received on all these connections onto the serial TX and it will +broadcast incoming characters from the serial RX to all connections. Use with caution! + +### Flashing an attached AVR/Arduino + +There are three options for reprogramming an attached AVR/Arduino microcontroller: +- Use avrdude and point it at port 23 of esp-link. Esp-link automatically detects the programming + sequence and issues a reset to the AVR. +- Use avrdude and point it at port 2323 of esp-link. This is the same as port 23 except that the + autodectection is not used and the reset happens because port 2323 is used +- Use curl or a similar tool to HTTP POST the firmware to esp-link. This uses the built-in + programmer, which only works for AVRs/Arduinos with the optiboot bootloader (which is std). + +To reprogram an Arduino / AVR microcontroller by pointing avrdude at port 23 or 2323 you +specify a serial port of the form `net:esp-link:23` in avrdude's -P option, where +`esp-link` is either the hostname of your esp-link or its IP address). +This is instead of specifying a serial port of the form /dev/ttyUSB0. +Esp-link detects that avrdude starts its connection with a flash synchronization sequence and sends a reset to the AVR microcontroller so it can switch into flash programming mode. +To reprogram using the HTTP POST method you need to first issue a POST to put optiboot into +programming mode: POST to `http://esp-link/pgm/sync`, this starts the process. Then check that +synchronization with optiboot has been achieved by issuing a GET to the same URL +(`http://esp-link/pgm/sync`). Repeat until you have sync (takes <500ms normally). Finally +issue a POST request to `http://esp-link/pgm/upload` with your hex file as POST data (raw, +not url-encoded or multipart-mime. Please look into the avrflash script for the curl command-line +details or use that script directly (`./avrflash esp-link.local my_sketch.hex`). + +### Flashing an attached ARM processor + You can reprogram NXP's LPC800-series and many other ARM processors as well by pointing your programmer similarly at the esp-link's port 23. For example, if you are using https://github.com/jeelabs/embello/tree/master/tools/uploader a command line like @@ -235,9 +298,12 @@ make esp-link issue the appropriate "ISP" and reset sequence to the microcontrol flash programming. If you use a different ARM programming tool it will work as well as long as it starts the connection with the `?\r\n` synchronization sequence. -Note that multiple connections to port 23 can be made simultaneously. The esp-link will -intermix characters received on all these connections onto the serial TX and it will -broadcast incoming characters from the serial RX to all connections. Use with caution! +### Flashing an attached esp8266 + +(This is not well tested, more details forthcoming...) +Yes, you can use esp-link running on one esp8266 module to flash another esp8266 module! +For this to work you need a special version of esptool.py that has support for serial over +telnet. Debug log --------- From ba347a83dc68a490dd2ec924ace32108e2b51b53 Mon Sep 17 00:00:00 2001 From: Thorsten von Eicken Date: Mon, 9 Nov 2015 21:20:37 -0800 Subject: [PATCH 11/43] readme tweaks --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index 7c96b3f..426abf5 100644 --- a/README.md +++ b/README.md @@ -286,6 +286,14 @@ synchronization with optiboot has been achieved by issuing a GET to the same URL issue a POST request to `http://esp-link/pgm/upload` with your hex file as POST data (raw, not url-encoded or multipart-mime. Please look into the avrflash script for the curl command-line details or use that script directly (`./avrflash esp-link.local my_sketch.hex`). +_Important_: after the initial sync request that resets the AVR you have 10 seconds to get to the +upload post or esp-link will time-out. So if you're manually entering curl commands have them +prepared so you can copy&paste! + +When to use which method? If port 23 works then go with that. If you have trouble getting sync +or it craps out in the middle too often then try the built-in programmer with the HTTP POST. +If your AVR doesn't use optiboot then use port 2323 since esp-link may not recognize the programming +sequence and not issue a reset if you use port 23. ### Flashing an attached ARM processor From 948990cefb26c26c92030fd1ca3eaf41ff25e322 Mon Sep 17 00:00:00 2001 From: Thorsten von Eicken Date: Tue, 10 Nov 2015 11:39:19 -0800 Subject: [PATCH 12/43] Add troubleshooting to readme --- README.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/README.md b/README.md index 426abf5..aff7623 100644 --- a/README.md +++ b/README.md @@ -295,6 +295,21 @@ or it craps out in the middle too often then try the built-in programmer with th If your AVR doesn't use optiboot then use port 2323 since esp-link may not recognize the programming sequence and not issue a reset if you use port 23. +If you are having trouble with the built-in programmer and see something like this: +``` +# ./avrflash 192.168.3.104 blink.hex +Error checking sync: FAILED to SYNC: abandoned after timeout, got: +:\xF/\x00\xCj\xCz\xCJ\xCZ\xC\xAÜ\xC\xAä\xC\xAÜ\xC\xAä\xC\xBì\xC\xBô\xC\xBì\xC\xBô\xC\xAÜ\xC\xAä\xC\x00\xC\x00\x00\x00 +\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00 +\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xF6\xF6\xF6\xF6\xC +``` +the most likely cause is a baud rate mismatch and/or a bad connection from the esp8266 to the AVRs reset line. +The baud rate used by esp-link is set on the uC Console web page. +The above garbage characters are most likely due to optiboot timing out and starting the sketch and then the sketch +sending data at a different baud rate than configured into esp-link. +Note that sketches don't necessarily use the same baud rate as optiboot, so you may have the correct baud rate configured +but reset isn't functioning, or reset may be functioning but the baud rate may be incorrect. + ### Flashing an attached ARM processor You can reprogram NXP's LPC800-series and many other ARM processors as well by pointing your From 14e206eb3f32ca38649f7d89432291c22377bc99 Mon Sep 17 00:00:00 2001 From: Thorsten von Eicken Date: Tue, 10 Nov 2015 12:15:23 -0800 Subject: [PATCH 13/43] Added info to readme --- README.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/README.md b/README.md index aff7623..09c81c4 100644 --- a/README.md +++ b/README.md @@ -310,6 +310,17 @@ sending data at a different baud rate than configured into esp-link. Note that sketches don't necessarily use the same baud rate as optiboot, so you may have the correct baud rate configured but reset isn't functioning, or reset may be functioning but the baud rate may be incorrect. +The output of a successful flash using the built-in programmer looks like this: +``` +Success. 3098 bytes in 0.8s, 3674B/s 63% efficient +``` +This says that the sketch comprises 3098 bytes of flash, sas written in 0.8 seconds (excludes the initial sync time), +and the 3098 bytes were flashed at a rate of 3674 bytes per second. +The efficiency measure is the ratio of the actual rate to the serial baud rate, in this case 57600 baud, +thus 3674/5760 = 0.63 (there are 10 baud per character). +The efficiency is not 100% because there is protocol overhead (such as sync, record type, and length characaters) +and there is dead time waiting for an ack or preparing the next record to be sent. + ### Flashing an attached ARM processor You can reprogram NXP's LPC800-series and many other ARM processors as well by pointing your From 86d6638e531f31a7dbb39b86980b2c6b3d67b7b7 Mon Sep 17 00:00:00 2001 From: Thorsten von Eicken Date: Tue, 10 Nov 2015 23:47:14 -0800 Subject: [PATCH 14/43] add system name & description to home page --- esp-link/cgi.c | 4 +++- esp-link/cgimqtt.c | 4 ++++ esp-link/cgimqtt.h | 1 + esp-link/config.c | 1 + esp-link/config.h | 3 ++- esp-link/main.c | 54 +++++++++++++++++++++++++++++++++++++++++++++- html/home.html | 39 +++++++++++++++++++++++---------- html/style.css | 6 +++++- html/ui.js | 49 +++++++++++++++++++++++++++++++++++++++++ httpd/httpd.c | 8 ++++++- 10 files changed, 153 insertions(+), 16 deletions(-) diff --git a/esp-link/cgi.c b/esp-link/cgi.c index bd2a074..f2881cf 100644 --- a/esp-link/cgi.c +++ b/esp-link/cgi.c @@ -16,6 +16,7 @@ Some random cgi routines. #include #include "cgi.h" +#include "config.h" void ICACHE_FLASH_ATTR noCacheHeaders(HttpdConnData *connData, int code) { @@ -158,7 +159,8 @@ int ICACHE_FLASH_ATTR cgiMenu(HttpdConnData *connData) { "\"REST/MQTT\", \"/mqtt.html\"," #endif "\"Debug log\", \"/log.html\" ],\n" - " \"version\": \"%s\" }", esp_link_version); + " \"version\": \"%s\"," + "\"name\":\"%s\"}", esp_link_version, flashConfig.sys_name); httpdSend(connData, buff, -1); return HTTPD_CGI_DONE; } diff --git a/esp-link/cgimqtt.c b/esp-link/cgimqtt.c index d4f7100..627d679 100644 --- a/esp-link/cgimqtt.c +++ b/esp-link/cgimqtt.c @@ -11,6 +11,10 @@ static char *mqtt_states[] = { "disconnected", "reconnecting", "connecting", "connected", }; +char *mqttState(void) { + return mqtt_states[mqttClient.connState]; +} + // Cgi to return MQTT settings int ICACHE_FLASH_ATTR cgiMqttGet(HttpdConnData *connData) { char buff[1024]; diff --git a/esp-link/cgimqtt.h b/esp-link/cgimqtt.h index ef58f38..b720fac 100644 --- a/esp-link/cgimqtt.h +++ b/esp-link/cgimqtt.h @@ -4,6 +4,7 @@ #include "httpd.h" int cgiMqtt(HttpdConnData *connData); +char *mqttState(void); #endif // CGIMQTT_H #endif // MQTT diff --git a/esp-link/config.c b/esp-link/config.c index eb70a0f..c2253dd 100644 --- a/esp-link/config.c +++ b/esp-link/config.c @@ -22,6 +22,7 @@ FlashConfig flashDefault = { 2, 1, // mqtt_timeout, mqtt_clean_session 1883, 60, // mqtt port, mqtt_keepalive "\0", "\0", "\0", "\0", "\0", // mqtt host, client_id, user, password, status-topic + "\0", "\0", // sys name, sys description }; typedef union { diff --git a/esp-link/config.h b/esp-link/config.h index 5a67058..b26f7b6 100644 --- a/esp-link/config.h +++ b/esp-link/config.h @@ -18,11 +18,12 @@ typedef struct { char api_key[48]; // RSSI submission API key (Grovestreams for now) uint8_t slip_enable, mqtt_enable, // SLIP protocol, MQTT client mqtt_status_enable, // MQTT status reporting - mqtt_timeout, // MQTT send timeout + mqtt_timeout, // MQTT send timeout mqtt_clean_session; // MQTT clean session uint16_t mqtt_port, mqtt_keepalive; // MQTT Host port, MQTT Keepalive timer char mqtt_host[32], mqtt_clientid[48], mqtt_username[32], mqtt_password[32]; char mqtt_status_topic[32]; + char sys_name[12], sys_descr[128]; // informal system name and description } FlashConfig; extern FlashConfig flashConfig; diff --git a/esp-link/main.c b/esp-link/main.c index a481c6d..9fab6a9 100644 --- a/esp-link/main.c +++ b/esp-link/main.c @@ -31,6 +31,9 @@ #include "log.h" #include +static int ICACHE_FLASH_ATTR cgiSystemInfo(HttpdConnData *connData); +static int ICACHE_FLASH_ATTR cgiSystemSet(HttpdConnData *connData); + /* This is the main url->function dispatching data struct. In short, it's a struct with various URLs plus their handlers. The handlers can @@ -64,8 +67,9 @@ HttpdBuiltInUrl builtInUrls[] = { { "/wifi/connstatus", cgiWiFiConnStatus, NULL }, { "/wifi/setmode", cgiWiFiSetMode, NULL }, { "/wifi/special", cgiWiFiSpecial, NULL }, + { "/system/info", cgiSystemInfo, NULL }, + { "/system/update", cgiSystemSet, NULL }, { "/pins", cgiPins, NULL }, - { "/tcpclient", cgiTcp, NULL }, #ifdef MQTT { "/mqtt", cgiMqtt, NULL }, #endif @@ -97,6 +101,53 @@ static char *flash_maps[] = { "2MB:1024/1024", "4MB:1024/1024" }; +// Cgi to return various System information +static int ICACHE_FLASH_ATTR cgiSystemInfo(HttpdConnData *connData) { + char buff[1024]; + + if (connData->conn==NULL) return HTTPD_CGI_DONE; // Connection aborted. Clean up. + + uint8 part_id = system_upgrade_userbin_check(); + uint32_t fid = spi_flash_get_id(); + struct rst_info *rst_info = system_get_rst_info(); + + os_sprintf(buff, "{\"name\": \"%s\", \"reset cause\": \"%d=%s\", " + "\"size\": \"%s\"," "\"id\": \"0x%02lX 0x%04lX\"," "\"partition\": \"%s\"," + "\"slip\": \"%s\"," "\"mqtt\": \"%s/%s\"," "\"baud\": \"%ld\"," + "\"description\": \"%s\"" "}", + flashConfig.sys_name, rst_info->reason, rst_codes[rst_info->reason], + flash_maps[system_get_flash_size_map()], fid & 0xff, (fid&0xff00)|((fid>>16)&0xff), + part_id ? "user2.bin" : "user1.bin", + flashConfig.slip_enable ? "enabled" : "disabled", + flashConfig.mqtt_enable ? "enabled" : "disabled", + mqttState(), flashConfig.baud_rate, flashConfig.sys_descr + ); + + jsonHeader(connData, 200); + httpdSend(connData, buff, -1); + return HTTPD_CGI_DONE; +} + +// Cgi to update system info (name/description) +static int ICACHE_FLASH_ATTR cgiSystemSet(HttpdConnData *connData) { + if (connData->conn==NULL) return HTTPD_CGI_DONE; // Connection aborted. Clean up. + + int8_t status = 0; + status |= getStringArg(connData, "name", flashConfig.sys_name, sizeof(flashConfig.sys_name)); + status |= getStringArg(connData, "description", flashConfig.sys_descr, sizeof(flashConfig.sys_descr)); + if (status < 0) return HTTPD_CGI_DONE; // getStringArg has produced an error response + + if (configSave()) { + httpdStartResponse(connData, 204); + httpdEndHeaders(connData); + } else { + httpdStartResponse(connData, 500); + httpdEndHeaders(connData); + httpdSend(connData, "Failed to save config", -1); + } + return HTTPD_CGI_DONE; +} + extern void app_init(void); extern void mqtt_client_init(void); @@ -120,6 +171,7 @@ void user_init(void) { os_delay_us(10000L); os_printf("\n\n** %s\n", esp_link_version); os_printf("Flash config restore %s\n", restoreOk ? "ok" : "*FAILED*"); + if (flashConfig.sys_name[0] == 0) os_strcpy(flashConfig.sys_name, "nameme"); #if defined(STA_SSID) && defined(STA_PASS) int x = wifi_get_opmode() & 0x3; diff --git a/html/home.html b/html/home.html index e7454f9..ca31c58 100644 --- a/html/home.html +++ b/html/home.html @@ -8,21 +8,20 @@
-

The JeeLabs esp-link firmware bridges the ESP8266 serial port to Wifi and can +

The JeeLabs esp-link firmware bridges the ESP8266 + serial port to Wifi and can program microcontrollers over the serial port, in particular Arduinos, AVRs, and - NXP's LPC800 and other ARM processors.

-

Program an Arduino/AVR using avrdude using a command - line similar to:

-
/home/arduino-1.0.5/hardware/tools/avrdude \
+ NXP's LPC800 and other ARM processors. Typical avrdude command line to + program an Arduino:

+
/home/arduino/hardware/tools/avrdude \
  -DV -patmega328p -Pnet:esp-link.local:23 -carduino -b115200 -U \
-   -C /home/arduino-1.0.5/hardware/tools/avrdude.conf flash:w:my_sketch.hex:i +   -C /home/arduino/hardware/tools/avrdude.conf flash:w:my_sketch.hex:i

where -Pnet:esp-link.local:23 tells avrdude to connect to port 23 of esp-link. - You can substitute the IP address of your esp-link for esp-link.local if necessary.

-

Please refer to + You can substitute the IP address of your esp-link for esp-link.local if necessary. + Please refer to the online README - for up-to-date help and to the forthcoming - JeeLabs blog for an intro to the codebase.

+ for up-to-date help.

@@ -37,7 +36,22 @@ Wifi status Wifi address Configured hostname - + +
+
+

Esp-link summary

+
+ + + + + + + + + +
@@ -56,8 +70,11 @@ diff --git a/html/style.css b/html/style.css index eba050f..59e2c3e 100644 --- a/html/style.css +++ b/html/style.css @@ -3,10 +3,14 @@ html, button, input, select, textarea, .pure-g [class *= "pure-u"] { font-family: sans-serif; } -input[type="text"], input[type="password"] { +input[type="text"], input[type="password"], textarea { width: 100%; } +input[type="text"]:disabled, textarea:disabled { + cursor: pointer; +} + input[type=checkbox] { float: left; margin: .35em 0.4em; diff --git a/html/ui.js b/html/ui.js index 16d6e7f..9085cdc 100644 --- a/html/ui.js +++ b/html/ui.js @@ -226,6 +226,7 @@ onLoad(function() {
\ \  esp-link\ +
\ \
\
\ @@ -258,6 +259,9 @@ onLoad(function() { v = $("#version"); if (v != null) { v.innerHTML = data.version; } + + n = $("#sysname"); + if (n != null) { n.innerHTML = data.name; } }, function() { setTimeout(getMenu, 1000); }); }; getMenu(); @@ -285,6 +289,51 @@ function getWifiInfo() { function(s, st) { window.setTimeout(getWifiInfo, 1000); }); } +//===== System info + +function showSystemInfo(data) { + Object.keys(data).forEach(function(v) { + el = $("#system-" + v); + if (el != null) { + if (el.nodeName === "INPUT") el.value = data[v]; + else el.innerHTML = data[v]; + } + }); + $("#system-spinner").setAttribute("hidden", ""); + $("#system-table").removeAttribute("hidden"); + currAp = data.ssid; +} + +function getSystemInfo() { + ajaxJson('GET', "/system/info", showSystemInfo, + function(s, st) { window.setTimeout(getSystemInfo, 1000); }); +} + +function enableInput(el) { + el.disabled = false; + el.select(); + return false; +} +function submitInput(klass, id, v) { + console.log("Submit POST /"+klass+"/update?"+id+"="+v); + $("#"+klass+"-"+id).disabled = true; + ajaxSpin("POST", "/"+klass+"/update?"+id+"="+v, function() { + showNotification(id + " changed to " + v); + }, function() { + showWarning(id + " change failed"); + }); + return false; +} +function makeAjaxInput(klass, field) { + var el = $("#"+klass+"-"+field); + bnd(el.parentElement, "click", function(){return enableInput(el);}); + bnd(el, "blur", function(){return submitInput(klass,field,el.value);}); + bnd(el, "keyup", function(ev){ + if ((ev||window.event).keyCode==13) return submitInput(klass,field,el.value); + }); +} + + //===== Notifications function showWarning(text) { diff --git a/httpd/httpd.c b/httpd/httpd.c index e0345d7..c5815a6 100644 --- a/httpd/httpd.c +++ b/httpd/httpd.c @@ -17,6 +17,8 @@ Esp8266 http server - core routines #include #include "httpd.h" +#define HTTPD_DBG + //Max length of request head #define MAX_HEAD_LEN 1024 @@ -394,9 +396,13 @@ static void ICACHE_FLASH_ATTR httpdProcessRequest(HttpdConnData *conn) { if (conn->post) conn->post->len = 0; // skip any remaining receives return; } - else if (r == HTTPD_CGI_NOTFOUND || r == HTTPD_CGI_AUTHENTICATED) { + else { + if (!(r == HTTPD_CGI_NOTFOUND || r == HTTPD_CGI_AUTHENTICATED)) { + os_printf("%shandler for %s returned invalid result %d\n", connStr, conn->url, r); + } //URL doesn't want to handle the request: either the data isn't found or there's no //need to generate a login screen. + conn->cgi = NULL; // force lookup again i++; //look at next url the next iteration of the loop. } } From 413ff5bcf1ced20034ba4ff54f0e3085fb89061b Mon Sep 17 00:00:00 2001 From: Thorsten von Eicken Date: Tue, 10 Nov 2015 23:49:19 -0800 Subject: [PATCH 15/43] disable httpd debug log again --- httpd/httpd.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httpd/httpd.c b/httpd/httpd.c index c5815a6..22da6bf 100644 --- a/httpd/httpd.c +++ b/httpd/httpd.c @@ -17,7 +17,7 @@ Esp8266 http server - core routines #include #include "httpd.h" -#define HTTPD_DBG +//#define HTTPD_DBG //Max length of request head From 6b3fb4e90b264f85e2c01a681ef01c8f2be78ba7 Mon Sep 17 00:00:00 2001 From: Thorsten von Eicken Date: Tue, 10 Nov 2015 23:56:02 -0800 Subject: [PATCH 16/43] update readme with reference to v2.1.beta2 --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 09c81c4..57045b4 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ Many thanks to https://github.com/brunnels for contributions around the espduino ###[Releases](https://github.com/jeelabs/esp-link/releases) -- [V2.1.beta1](https://github.com/jeelabs/esp-link/releases/tag/v2.1.beta1) has the new built-in +- [V2.1.beta2](https://github.com/jeelabs/esp-link/releases/tag/v2.1.beta2) has the new built-in stk500v1 programmer and works on all modules (esp-01 through esp-12). This is still beta-ware! - [V2.0.rc1](https://github.com/jeelabs/esp-link/releases/tag/v2.0.rc1) has REST support but requires a 1MByte or 4MByte ESP8266 flash, e.g. esp-12 or wroom-02. Despite being labeled From e429c0bb1c8de5b77ed771976ae3c70f543bbd2b Mon Sep 17 00:00:00 2001 From: Thorsten von Eicken Date: Thu, 12 Nov 2015 00:04:27 -0800 Subject: [PATCH 17/43] Improve click-to-edit fields --- Makefile | 2 +- esp-link/cgioptiboot.c | 6 ++- esp-link/config.h | 2 +- esp-link/main.c | 2 +- html/home.html | 22 ++++++----- html/style.css | 30 ++++++++++++--- html/ui.js | 84 +++++++++++++++++++++++++++--------------- 7 files changed, 100 insertions(+), 48 deletions(-) diff --git a/Makefile b/Makefile index 10f3beb..78342c1 100644 --- a/Makefile +++ b/Makefile @@ -370,7 +370,7 @@ ifeq ("$(COMPRESS_W_HTMLCOMPRESSOR)","yes") $(WIFI_PATH)*.html $(Q) echo "Compression assets with yui-compressor. This may take a while..." $(Q) for file in `find html_compressed -type f -name "*.js"`; do \ - java -jar tools/$(YUI_COMPRESSOR) $$file -o $$file; \ + java -jar tools/$(YUI_COMPRESSOR) $$file --line-break 0 -o $$file; \ done $(Q) for file in `find html_compressed -type f -name "*.css"`; do \ java -jar tools/$(YUI_COMPRESSOR) $$file -o $$file; \ diff --git a/esp-link/cgioptiboot.c b/esp-link/cgioptiboot.c index dfeb360..bd2dd2a 100644 --- a/esp-link/cgioptiboot.c +++ b/esp-link/cgioptiboot.c @@ -84,6 +84,8 @@ static void ICACHE_FLASH_ATTR optibootInit() { DBG("OB init\n"); } +// append one string to another but visually escape non-printing characters in the second +// string using \x00 hex notation, max is the max chars in the concatenated string. void ICACHE_FLASH_ATTR appendPretty(char *buf, char *raw, int max) { int off = strlen(buf); int i = 0; @@ -100,8 +102,8 @@ void ICACHE_FLASH_ATTR appendPretty(char *buf, char *raw, int max) { } else { buf[off++] = '\\'; buf[off++] = 'x'; - buf[off++] = '0'+(c>>4)+((c>>4)>9?7:0); - buf[off++] = '0'+(c&0xff)+((c&0xff)>9?7:0); + buf[off++] = '0'+(unsigned char)((c>>4)+((c>>4)>9?7:0)); + buf[off++] = '0'+(unsigned char)((c&0xff)+((c&0xff)>9?7:0)); } } buf[off] = 0; diff --git a/esp-link/config.h b/esp-link/config.h index b26f7b6..f7cb2b9 100644 --- a/esp-link/config.h +++ b/esp-link/config.h @@ -23,7 +23,7 @@ typedef struct { uint16_t mqtt_port, mqtt_keepalive; // MQTT Host port, MQTT Keepalive timer char mqtt_host[32], mqtt_clientid[48], mqtt_username[32], mqtt_password[32]; char mqtt_status_topic[32]; - char sys_name[12], sys_descr[128]; // informal system name and description + char sys_name[13], sys_descr[129]; // informal system name and description } FlashConfig; extern FlashConfig flashConfig; diff --git a/esp-link/main.c b/esp-link/main.c index 9fab6a9..1744bd0 100644 --- a/esp-link/main.c +++ b/esp-link/main.c @@ -171,7 +171,7 @@ void user_init(void) { os_delay_us(10000L); os_printf("\n\n** %s\n", esp_link_version); os_printf("Flash config restore %s\n", restoreOk ? "ok" : "*FAILED*"); - if (flashConfig.sys_name[0] == 0) os_strcpy(flashConfig.sys_name, "nameme"); + if (flashConfig.sys_name[0] == 0) os_strcpy(flashConfig.sys_name, "name-me"); #if defined(STA_SSID) && defined(STA_PASS) int x = wifi_get_opmode() & 0x3; diff --git a/html/home.html b/html/home.html index ca31c58..69dccff 100644 --- a/html/home.html +++ b/html/home.html @@ -42,15 +42,20 @@

Esp-link summary

- - - - - - - + + + + + + +
+ + + +
+
@@ -70,7 +75,6 @@ diff --git a/serial/console.c b/serial/console.c index d0d0dd7..68b5b55 100644 --- a/serial/console.c +++ b/serial/console.c @@ -81,6 +81,23 @@ ajaxConsoleBaud(HttpdConnData *connData) { return HTTPD_CGI_DONE; } +int ICACHE_FLASH_ATTR +ajaxConsoleSend(HttpdConnData *connData) { + if (connData->conn==NULL) return HTTPD_CGI_DONE; // Connection aborted. Clean up. + char buff[2048]; + int len, status = 400; + + // figure out where to start in buffer based on URI param + len = httpdFindArg(connData->getArgs, "text", buff, sizeof(buff)); + if (len > 0) { + uart0_tx_buffer(buff, len); + status = 200; + } + + jsonHeader(connData, status); + return HTTPD_CGI_DONE; +} + int ICACHE_FLASH_ATTR ajaxConsole(HttpdConnData *connData) { if (connData->conn==NULL) return HTTPD_CGI_DONE; // Connection aborted. Clean up. diff --git a/serial/console.h b/serial/console.h index dc58a26..6225c0a 100644 --- a/serial/console.h +++ b/serial/console.h @@ -8,6 +8,7 @@ void ICACHE_FLASH_ATTR console_write_char(char c); int ajaxConsole(HttpdConnData *connData); int ajaxConsoleReset(HttpdConnData *connData); int ajaxConsoleBaud(HttpdConnData *connData); +int ajaxConsoleSend(HttpdConnData *connData); int tplConsole(HttpdConnData *connData, char *token, void **arg); #endif From 52b78a42b974a5de77df65d78227f8a293d3da8f Mon Sep 17 00:00:00 2001 From: Kayo Phoenix Date: Sat, 14 Nov 2015 00:53:40 +0500 Subject: [PATCH 20/43] Moved console send js from console.html to console.js --- html/console.html | 84 +-------------------------------------------- html/console.js | 86 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+), 83 deletions(-) diff --git a/html/console.html b/html/console.html index c3b9fab..201f82a 100644 --- a/html/console.html +++ b/html/console.html @@ -58,90 +58,8 @@ function(data) { showRate(data.rate); }, function(s, st) { showNotification(st); } ); - - var sendHistory = $("#send-history"); - var inputText = $("#input-text"); - var inputAddCr = $("#input-add-cr"); - var inputAddLf = $("#input-add-lf"); - - function findHistory(text) { - for (var i = 0; i < sendHistory.children.length; i++) { - if (text == sendHistory.children[i].value) { - return i; - } - } - return null; - } - - function loadHistory(idx) { - sendHistory.value = sendHistory.children[idx].value; - inputText.value = sendHistory.children[idx].value; - } - - function navHistory(rel) { - var idx = findHistory(sendHistory.value) + rel; - if (idx < 0) { - idx = sendHistory.children.length - 1; - } - if (idx >= sendHistory.children.length) { - idx = 0; - } - loadHistory(idx); - } - - sendHistory.addEventListener("change", function(e) { - inputText.value = sendHistory.value; - }); - function pushHistory(text) { - var idx = findHistory(text); - if (idx !== null) { - loadHistory(idx); - return false; - } - var newOption = m(''); - newOption.value = text; - sendHistory.appendChild(newOption); - sendHistory.value = text; - for (; sendHistory.children.length > 15; ) { - sendHistory.removeChild(sendHistory.children[0]); - } - return true; - } - - inputText.addEventListener("keydown", function(e) { - switch (e.keyCode) { - case 38: /* the up arrow key pressed */ - e.preventDefault(); - navHistory(-1); - break; - case 40: /* the down arrow key pressed */ - e.preventDefault(); - navHistory(+1); - break; - case 27: /* the escape key pressed */ - e.preventDefault(); - inputText.value = ""; - sendHistory.value = ""; - break; - case 13: /* the enter key pressed */ - e.preventDefault(); - var text = inputText.value; - if (inputAddCr.checked) text += '\r'; - if (inputAddLf.checked) text += '\n'; - ajaxSpin('POST', "/console/send?text=" + encodeURIComponent(text), - function(resp) { showNotification("uC sent"); pushHistory(inputText.value); }, - function(s, st) { showWarning("Error sending text to uC"); } - ); - break; - } - }); + consoleSendInit(); }); diff --git a/html/console.js b/html/console.js index 87f7d1a..1080d0a 100644 --- a/html/console.js +++ b/html/console.js @@ -59,6 +59,92 @@ function baudButton(baud) { }); } +function consoleSendInit() { + var sendHistory = $("#send-history"); + var inputText = $("#input-text"); + var inputAddCr = $("#input-add-cr"); + var inputAddLf = $("#input-add-lf"); + + function findHistory(text) { + for (var i = 0; i < sendHistory.children.length; i++) { + if (text == sendHistory.children[i].value) { + return i; + } + } + return null; + } + + function loadHistory(idx) { + sendHistory.value = sendHistory.children[idx].value; + inputText.value = sendHistory.children[idx].value; + } + + function navHistory(rel) { + var idx = findHistory(sendHistory.value) + rel; + if (idx < 0) { + idx = sendHistory.children.length - 1; + } + if (idx >= sendHistory.children.length) { + idx = 0; + } + loadHistory(idx); + } + + sendHistory.addEventListener("change", function(e) { + inputText.value = sendHistory.value; + }); + + function pushHistory(text) { + var idx = findHistory(text); + if (idx !== null) { + loadHistory(idx); + return false; + } + var newOption = m(''); + newOption.value = text; + sendHistory.appendChild(newOption); + sendHistory.value = text; + for (; sendHistory.children.length > 15; ) { + sendHistory.removeChild(sendHistory.children[0]); + } + return true; + } + + inputText.addEventListener("keydown", function(e) { + switch (e.keyCode) { + case 38: /* the up arrow key pressed */ + e.preventDefault(); + navHistory(-1); + break; + case 40: /* the down arrow key pressed */ + e.preventDefault(); + navHistory(+1); + break; + case 27: /* the escape key pressed */ + e.preventDefault(); + inputText.value = ""; + sendHistory.value = ""; + break; + case 13: /* the enter key pressed */ + e.preventDefault(); + var text = inputText.value; + if (inputAddCr.checked) text += '\r'; + if (inputAddLf.checked) text += '\n'; + ajaxSpin('POST', "/console/send?text=" + encodeURIComponent(text), + function(resp) { showNotification("uC sent"); pushHistory(inputText.value); }, + function(s, st) { showWarning("Error sending text to uC"); } + ); + break; + } + }); +} + //===== Log page function showDbgMode(mode) { From a87645dad4a04eda4dfb335a45628b6d2a28281a Mon Sep 17 00:00:00 2001 From: Thorsten von Eicken Date: Fri, 13 Nov 2015 08:33:55 -0800 Subject: [PATCH 21/43] extend time before switching to STA-only; re-enable 11n; clean-up debug printing --- esp-link/cgiwifi.c | 124 ++++++++++++++++----------------------------- 1 file changed, 43 insertions(+), 81 deletions(-) diff --git a/esp-link/cgiwifi.c b/esp-link/cgiwifi.c index 821328a..1fe7ade 100644 --- a/esp-link/cgiwifi.c +++ b/esp-link/cgiwifi.c @@ -21,8 +21,11 @@ Cgi/template routines for the /wifi url. #include "config.h" #include "log.h" -//#define SLEEP_MODE LIGHT_SLEEP_T -#define SLEEP_MODE MODEM_SLEEP_T +#ifdef CGIWIFI_DBG +#define DBG(format, ...) os_printf(format, ## __VA_ARGS__) +#else +#define DBG(format, ...) do { } while(0) +#endif // ===== wifi status change callbacks static WifiStateChangeCb wifi_state_change_cb[4]; @@ -55,48 +58,36 @@ static void ICACHE_FLASH_ATTR wifiHandleEventCb(System_Event_t *evt) { case EVENT_STAMODE_CONNECTED: wifiState = wifiIsConnected; wifiReason = 0; -#ifdef CGIWIFI_DBG - os_printf("Wifi connected to ssid %s, ch %d\n", evt->event_info.connected.ssid, + DBG("Wifi connected to ssid %s, ch %d\n", evt->event_info.connected.ssid, evt->event_info.connected.channel); -#endif statusWifiUpdate(wifiState); break; case EVENT_STAMODE_DISCONNECTED: wifiState = wifiIsDisconnected; wifiReason = evt->event_info.disconnected.reason; -#ifdef CGIWIFI_DBG - os_printf("Wifi disconnected from ssid %s, reason %s (%d)\n", + DBG("Wifi disconnected from ssid %s, reason %s (%d)\n", evt->event_info.disconnected.ssid, wifiGetReason(), evt->event_info.disconnected.reason); -#endif statusWifiUpdate(wifiState); break; case EVENT_STAMODE_AUTHMODE_CHANGE: -#ifdef CGIWIFI_DBG - os_printf("Wifi auth mode: %d -> %d\n", + DBG("Wifi auth mode: %d -> %d\n", evt->event_info.auth_change.old_mode, evt->event_info.auth_change.new_mode); -#endif break; case EVENT_STAMODE_GOT_IP: wifiState = wifiGotIP; wifiReason = 0; -#ifdef CGIWIFI_DBG - os_printf("Wifi got ip:" IPSTR ",mask:" IPSTR ",gw:" IPSTR "\n", + DBG("Wifi got ip:" IPSTR ",mask:" IPSTR ",gw:" IPSTR "\n", IP2STR(&evt->event_info.got_ip.ip), IP2STR(&evt->event_info.got_ip.mask), IP2STR(&evt->event_info.got_ip.gw)); -#endif statusWifiUpdate(wifiState); break; case EVENT_SOFTAPMODE_STACONNECTED: -#ifdef CGIWIFI_DBG - os_printf("Wifi AP: station " MACSTR " joined, AID = %d\n", + DBG("Wifi AP: station " MACSTR " joined, AID = %d\n", MAC2STR(evt->event_info.sta_connected.mac), evt->event_info.sta_connected.aid); -#endif break; case EVENT_SOFTAPMODE_STADISCONNECTED: -#ifdef CGIWIFI_DBG - os_printf("Wifi AP: station " MACSTR " left, AID = %d\n", + DBG("Wifi AP: station " MACSTR " left, AID = %d\n", MAC2STR(evt->event_info.sta_disconnected.mac), evt->event_info.sta_disconnected.aid); -#endif break; default: break; @@ -116,9 +107,7 @@ wifiAddStateChangeCb(WifiStateChangeCb cb) { return; } } -#ifdef CGIWIFI_DBG - os_printf("WIFI: max state change cb count exceeded\n"); -#endif + DBG("WIFI: max state change cb count exceeded\n"); } // ===== wifi scanning @@ -147,9 +136,7 @@ void ICACHE_FLASH_ATTR wifiScanDoneCb(void *arg, STATUS status) { struct bss_info *bss_link = (struct bss_info *)arg; if (status!=OK) { -#ifdef CGIWIFI_DBG - os_printf("wifiScanDoneCb status=%d\n", status); -#endif + DBG("wifiScanDoneCb status=%d\n", status); cgiWifiAps.scanInProgress=0; return; } @@ -169,9 +156,7 @@ void ICACHE_FLASH_ATTR wifiScanDoneCb(void *arg, STATUS status) { //Allocate memory for access point data cgiWifiAps.apData=(ApData **)os_malloc(sizeof(ApData *)*n); cgiWifiAps.noAps=n; -#ifdef CGIWIFI_DBG - os_printf("Scan done: found %d APs\n", n); -#endif + DBG("Scan done: found %d APs\n", n); //Copy access point data to the static struct n=0; @@ -180,9 +165,7 @@ void ICACHE_FLASH_ATTR wifiScanDoneCb(void *arg, STATUS status) { if (n>=cgiWifiAps.noAps) { //This means the bss_link changed under our nose. Shouldn't happen! //Break because otherwise we will write in unallocated memory. -#ifdef CGIWIFI_DBG - os_printf("Huh? I have more than the allocated %d aps!\n", cgiWifiAps.noAps); -#endif + DBG("Huh? I have more than the allocated %d aps!\n", cgiWifiAps.noAps); break; } //Save the ap data. @@ -190,9 +173,7 @@ void ICACHE_FLASH_ATTR wifiScanDoneCb(void *arg, STATUS status) { cgiWifiAps.apData[n]->rssi=bss_link->rssi; cgiWifiAps.apData[n]->enc=bss_link->authmode; strncpy(cgiWifiAps.apData[n]->ssid, (char*)bss_link->ssid, 32); -#ifdef CGIWIFI_DBG - os_printf("bss%d: %s (%d)\n", n+1, (char*)bss_link->ssid, bss_link->rssi); -#endif + DBG("bss%d: %s (%d)\n", n+1, (char*)bss_link->ssid, bss_link->rssi); bss_link = bss_link->next.stqe_next; n++; @@ -203,9 +184,7 @@ void ICACHE_FLASH_ATTR wifiScanDoneCb(void *arg, STATUS status) { static ETSTimer scanTimer; static void ICACHE_FLASH_ATTR scanStartCb(void *arg) { -#ifdef CGIWIFI_DBG - os_printf("Starting a scan\n"); -#endif + DBG("Starting a scan\n"); wifi_station_scan(NULL, wifiScanDoneCb); } @@ -216,7 +195,7 @@ static int ICACHE_FLASH_ATTR cgiWiFiStartScan(HttpdConnData *connData) { cgiWifiAps.scanInProgress = 1; os_timer_disarm(&scanTimer); os_timer_setfn(&scanTimer, scanStartCb, NULL); - os_timer_arm(&scanTimer, 1000, 0); + os_timer_arm(&scanTimer, 200, 0); } return HTTPD_CGI_DONE; } @@ -290,35 +269,26 @@ static ETSTimer resetTimer; static void ICACHE_FLASH_ATTR resetTimerCb(void *arg) { int x = wifi_station_get_connect_status(); int m = wifi_get_opmode() & 0x3; -#ifdef CGIWIFI_DBG - os_printf("Wifi check: mode=%s status=%d\n", wifiMode[m], x); -#endif + DBG("Wifi check: mode=%s status=%d\n", wifiMode[m], x); if (x == STATION_GOT_IP) { if (m != 1) { #ifdef CHANGE_TO_STA // We're happily connected, go to STA mode -#ifdef CGIWIFI_DBG - os_printf("Wifi got IP. Going into STA mode..\n"); -#endif + DBG("Wifi got IP. Going into STA mode..\n"); wifi_set_opmode(1); - wifi_set_sleep_type(SLEEP_MODE); - os_timer_arm(&resetTimer, RESET_TIMEOUT, 0); + os_timer_arm(&resetTimer, RESET_TIMEOUT, 0); // check one more time after switching to STA-only #endif } log_uart(false); // no more resetTimer at this point, gotta use physical reset to recover if in trouble } else { if (m != 3) { -#ifdef CGIWIFI_DBG - os_printf("Wifi connect failed. Going into STA+AP mode..\n"); -#endif + DBG("Wifi connect failed. Going into STA+AP mode..\n"); wifi_set_opmode(3); } log_uart(true); -#ifdef CGIWIFI_DBG - os_printf("Enabling/continuing uart log\n"); -#endif + DBG("Enabling/continuing uart log\n"); os_timer_arm(&resetTimer, RESET_TIMEOUT, 0); } } @@ -330,17 +300,17 @@ static ETSTimer reassTimer; // Callback actually doing reassociation static void ICACHE_FLASH_ATTR reassTimerCb(void *arg) { -#ifdef CGIWIFI_DBG - os_printf("Wifi changing association\n"); -#endif + DBG("Wifi changing association\n"); wifi_station_disconnect(); stconf.bssid_set = 0; wifi_station_set_config(&stconf); wifi_station_connect(); - // Schedule check + // Schedule check, we give some extra time (4x) 'cause the reassociation can cause the AP + // to have to change channel, and then the client needs to follow before it can see the + // IP address os_timer_disarm(&resetTimer); os_timer_setfn(&resetTimer, resetTimerCb, NULL); - os_timer_arm(&resetTimer, RESET_TIMEOUT, 0); + os_timer_arm(&resetTimer, 4*RESET_TIMEOUT, 0); } // This cgi uses the routines above to connect to a specific access point with the @@ -358,14 +328,12 @@ int ICACHE_FLASH_ATTR cgiWiFiConnect(HttpdConnData *connData) { //Set to 0 if you want to disable the actual reconnecting bit os_strncpy((char*)stconf.ssid, essid, 32); os_strncpy((char*)stconf.password, passwd, 64); -#ifdef CGIWIFI_DBG - os_printf("Wifi try to connect to AP %s pw %s\n", essid, passwd); -#endif + DBG("Wifi try to connect to AP %s pw %s\n", essid, passwd); //Schedule disconnect/connect os_timer_disarm(&reassTimer); os_timer_setfn(&reassTimer, reassTimerCb, NULL); - os_timer_arm(&reassTimer, 1000, 0); + os_timer_arm(&reassTimer, 1000, 0); // 1 second for the response of this request to make it jsonHeader(connData, 200); } else { jsonHeader(connData, 400); @@ -421,9 +389,7 @@ static void ICACHE_FLASH_ATTR configWifiIP() { if (wifi_station_dhcpc_status() == DHCP_STARTED) wifi_station_dhcpc_stop(); wifi_station_dhcpc_start(); -#ifdef CGIWIFI_DBG - os_printf("Wifi uses DHCP, hostname=%s\n", flashConfig.hostname); -#endif + DBG("Wifi uses DHCP, hostname=%s\n", flashConfig.hostname); } else { // no DHCP, we got static network config! wifi_station_dhcpc_stop(); @@ -432,9 +398,7 @@ static void ICACHE_FLASH_ATTR configWifiIP() { ipi.netmask.addr = flashConfig.netmask; ipi.gw.addr = flashConfig.gateway; wifi_set_ip_info(0, &ipi); -#ifdef CGIWIFI_DBG - os_printf("Wifi uses static IP %d.%d.%d.%d\n", IP2STR(&ipi.ip.addr)); -#endif + DBG("Wifi uses static IP %d.%d.%d.%d\n", IP2STR(&ipi.ip.addr)); } #ifdef DEBUGIP debugIP(); @@ -497,7 +461,7 @@ int ICACHE_FLASH_ATTR cgiWiFiSpecial(HttpdConnData *connData) { // schedule change-over os_timer_disarm(&reassTimer); os_timer_setfn(&reassTimer, configWifiIP, NULL); - os_timer_arm(&reassTimer, 1000, 0); + os_timer_arm(&reassTimer, 1000, 0); // 1 second for the response of this request to make it // return redirect info jsonHeader(connData, 200); httpdSend(connData, url, -1); @@ -514,13 +478,10 @@ int ICACHE_FLASH_ATTR cgiWiFiSetMode(HttpdConnData *connData) { len=httpdFindArg(connData->getArgs, "mode", buff, sizeof(buff)); if (len!=0) { int m = atoi(buff); -#ifdef CGIWIFI_DBG - os_printf("Wifi switching to mode %d\n", m); -#endif + DBG("Wifi switching to mode %d\n", m); wifi_set_opmode(m&3); if (m == 1) { - wifi_set_sleep_type(SLEEP_MODE); - // STA-only mode, reset into STA+AP after a timeout + // STA-only mode, reset into STA+AP after a timeout if we don't get an IP address os_timer_disarm(&resetTimer); os_timer_setfn(&resetTimer, resetTimerCb, NULL); os_timer_arm(&resetTimer, RESET_TIMEOUT, 0); @@ -619,9 +580,7 @@ int ICACHE_FLASH_ATTR cgiWiFiConnStatus(HttpdConnData *connData) { #endif len += os_sprintf(buff+len, "\"x\":0}\n"); -#ifdef CGIWIFI_DBG - //os_printf(" -> %s\n", buff); -#endif + //DBG(" -> %s\n", buff); httpdSend(connData, buff, len); return HTTPD_CGI_DONE; } @@ -644,13 +603,16 @@ int ICACHE_FLASH_ATTR cgiWifiInfo(HttpdConnData *connData) { // Init the wireless, which consists of setting a timer if we expect to connect to an AP // so we can revert to STA+AP mode if we can't connect. void ICACHE_FLASH_ATTR wifiInit() { - wifi_set_phy_mode(2); -#ifdef CGIWIFI_DBG + // wifi_set_phy_mode(2); // limit to 802.11b/g 'cause n is flaky int x = wifi_get_opmode() & 0x3; - os_printf("Wifi init, mode=%s\n", wifiMode[x]); -#endif + DBG("Wifi init, mode=%s\n", wifiMode[x]); configWifiIP(); + // The default sleep mode should be modem_sleep, but we set it here explicitly for good + // measure. We can't use light_sleep because that powers off everthing and we would loose + // all connections. + wifi_set_sleep_type(MODEM_SLEEP_T); + wifi_set_event_handler_cb(wifiHandleEventCb); // check on the wifi in a few seconds to see whether we need to switch mode os_timer_disarm(&resetTimer); From 05d73e570d8d4a643ee8dc983bb99f164279eae3 Mon Sep 17 00:00:00 2001 From: Thorsten von Eicken Date: Fri, 13 Nov 2015 08:48:18 -0800 Subject: [PATCH 22/43] try to support hidden ssids --- html/wifi/wifi.html | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/html/wifi/wifi.html b/html/wifi/wifi.html index b6c142b..fda9ff1 100644 --- a/html/wifi/wifi.html +++ b/html/wifi/wifi.html @@ -28,6 +28,10 @@ enter the password, and hit the connect button...
Scanning...
+ From 20627272b577d870d4ac60b7549fee2db2834909 Mon Sep 17 00:00:00 2001 From: Thorsten von Eicken Date: Fri, 13 Nov 2015 21:35:05 -0800 Subject: [PATCH 23/43] add tgz to gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index a541909..e462231 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,4 @@ esp-link.sdf espfs/mkespfsimage/mman-win32/libmman.a .localhistory/ tools/ +*.tgz From cf7abfc59a46d888bb7823d1cf2e5adf8c4676ae Mon Sep 17 00:00:00 2001 From: Thorsten von Eicken Date: Fri, 13 Nov 2015 23:34:51 -0800 Subject: [PATCH 24/43] added first cut at mDNS --- esp-link/cgiwifi.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/esp-link/cgiwifi.c b/esp-link/cgiwifi.c index 1fe7ade..f35d73e 100644 --- a/esp-link/cgiwifi.c +++ b/esp-link/cgiwifi.c @@ -27,6 +27,8 @@ Cgi/template routines for the /wifi url. #define DBG(format, ...) do { } while(0) #endif +static void wifiStartMDNS(struct ip_addr); + // ===== wifi status change callbacks static WifiStateChangeCb wifi_state_change_cb[4]; @@ -80,6 +82,7 @@ static void ICACHE_FLASH_ATTR wifiHandleEventCb(System_Event_t *evt) { IP2STR(&evt->event_info.got_ip.ip), IP2STR(&evt->event_info.got_ip.mask), IP2STR(&evt->event_info.got_ip.gw)); statusWifiUpdate(wifiState); + wifiStartMDNS(evt->event_info.got_ip.ip); break; case EVENT_SOFTAPMODE_STACONNECTED: DBG("Wifi AP: station " MACSTR " joined, AID = %d\n", @@ -110,6 +113,24 @@ wifiAddStateChangeCb(WifiStateChangeCb cb) { DBG("WIFI: max state change cb count exceeded\n"); } +static bool mdns_started = false; +static struct mdns_info mdns_info; + +// cannot allocate the info struct on the stack, it crashes! +static ICACHE_FLASH_ATTR +void wifiStartMDNS(struct ip_addr ip) { + if (!mdns_started) { + os_memset(&mdns_info, 0, sizeof(struct mdns_info)); + mdns_info.host_name = flashConfig.hostname; + mdns_info.server_name = "http", // service name + mdns_info.server_port = 80, // service port + mdns_info.ipAddr = ip.addr, + espconn_mdns_init(&mdns_info); + espconn_mdns_enable(); + mdns_started = true; + } +} + // ===== wifi scanning //WiFi access point data From 9920fd500e51588554ce2e435f94c38f904bf91e Mon Sep 17 00:00:00 2001 From: Thorsten von Eicken Date: Sat, 14 Nov 2015 00:21:39 -0800 Subject: [PATCH 25/43] make hidden ssid work minimally --- html/wifi/wifi.html | 4 ++-- html/wifi/wifi.js | 24 ++++++++++++++---------- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/html/wifi/wifi.html b/html/wifi/wifi.html index fda9ff1..4ddfc20 100644 --- a/html/wifi/wifi.html +++ b/html/wifi/wifi.html @@ -29,8 +29,8 @@
Scanning...
diff --git a/html/wifi/wifi.js b/html/wifi/wifi.js index d99be55..82254dc 100644 --- a/html/wifi/wifi.js +++ b/html/wifi/wifi.js @@ -41,7 +41,11 @@ function createInputForAp(ap) { function getSelectedEssid() { var e = document.forms.wifiform.elements; for (var i=0; i Date: Sat, 14 Nov 2015 23:35:59 -0800 Subject: [PATCH 26/43] disable mdns for now --- esp-link/cgiwifi.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/esp-link/cgiwifi.c b/esp-link/cgiwifi.c index f35d73e..e379b12 100644 --- a/esp-link/cgiwifi.c +++ b/esp-link/cgiwifi.c @@ -113,7 +113,7 @@ wifiAddStateChangeCb(WifiStateChangeCb cb) { DBG("WIFI: max state change cb count exceeded\n"); } -static bool mdns_started = false; +static bool mdns_started = true; static struct mdns_info mdns_info; // cannot allocate the info struct on the stack, it crashes! From 65583e6af6b91cfe5b5b8ad3cb7c7a52a0ad1c55 Mon Sep 17 00:00:00 2001 From: Thorsten von Eicken Date: Sat, 14 Nov 2015 23:37:41 -0800 Subject: [PATCH 27/43] add selection of uart1 to debug log --- esp-link/log.c | 59 +++++++++++++++++++++++-------------------------- esp-link/log.h | 7 +++--- esp-link/main.c | 2 +- html/log.html | 5 +++-- serial/uart.c | 3 --- 5 files changed, 36 insertions(+), 40 deletions(-) diff --git a/esp-link/log.c b/esp-link/log.c index 1425cb3..d7035c0 100644 --- a/esp-link/log.c +++ b/esp-link/log.c @@ -6,6 +6,12 @@ #include "config.h" #include "log.h" +#ifdef LOG_DBG +#define DBG(format, ...) os_printf(format, ## __VA_ARGS__) +#else +#define DBG(format, ...) do { } while(0) +#endif + // Web log for the esp8266 to replace outputting to uart1. // The web log has a 1KB circular in-memory buffer which os_printf prints into and // the HTTP handler simply displays the buffer content on a web page. @@ -18,30 +24,31 @@ static int log_pos; static bool log_no_uart; // start out printing to uart static bool log_newline; // at start of a new line -#define uart0_write_char uart1_write_char +// write to the uart designated for logging +static void uart_write_char(char c) { + if (flashConfig.log_mode == LOG_MODE_ON1) + uart1_write_char(c); + else + uart0_write_char(c); +} // called from wifi reset timer to turn UART on when we loose wifi and back off // when we connect to wifi AP. Here this is gated by the flash setting void ICACHE_FLASH_ATTR log_uart(bool enable) { - if (!enable && !log_no_uart && flashConfig.log_mode != LOG_MODE_ON) { + if (!enable && !log_no_uart && flashConfig.log_mode < LOG_MODE_ON0) { // we're asked to turn uart off, and uart is on, and the flash setting isn't always-on -#if 1 -#ifdef LOG_DBG - os_printf("Turning OFF uart log\n"); -#endif + DBG("Turning OFF uart log\n"); os_delay_us(4*1000L); // time for uart to flush log_no_uart = !enable; -#endif } else if (enable && log_no_uart && flashConfig.log_mode != LOG_MODE_OFF) { // we're asked to turn uart on, and uart is off, and the flash setting isn't always-off log_no_uart = !enable; -#ifdef LOG_DBG - os_printf("Turning ON uart log\n"); -#endif + DBG("Turning ON uart log\n"); } } +// write a character into the log buffer static void ICACHE_FLASH_ATTR log_write(char c) { log_buf[log_wr] = c; @@ -52,18 +59,7 @@ log_write(char c) { } } -#if 0 -static char ICACHE_FLASH_ATTR -log_read(void) { - char c = 0; - if (log_rd != log_wr) { - c = log_buf[log_rd]; - log_rd = (log_rd+1) % BUF_MAX; - } - return c; -} -#endif - +// write a character to the log buffer and the uart, and handle newlines specially static void ICACHE_FLASH_ATTR log_write_char(char c) { // log timestamp @@ -71,16 +67,16 @@ log_write_char(char c) { char buff[16]; int l = os_sprintf(buff, "%6d> ", (system_get_time()/1000)%1000000); if (!log_no_uart) - for (int i=0; igetArgs, "mode", buff, sizeof(buff)); if (len > 0) { int8_t mode = -1; - if (os_strcmp(buff, "auto") == 0) mode = LOG_MODE_AUTO; - if (os_strcmp(buff, "off") == 0) mode = LOG_MODE_OFF; - if (os_strcmp(buff, "on") == 0) mode = LOG_MODE_ON; + if (os_strcmp(buff, "auto") == 0) mode = LOG_MODE_AUTO; + if (os_strcmp(buff, "off") == 0) mode = LOG_MODE_OFF; + if (os_strcmp(buff, "on0") == 0) mode = LOG_MODE_ON0; + if (os_strcmp(buff, "on1") == 0) mode = LOG_MODE_ON1; if (mode >= 0) { flashConfig.log_mode = mode; - if (mode != LOG_MODE_AUTO) log_uart(mode == LOG_MODE_ON); + if (mode != LOG_MODE_AUTO) log_uart(mode >= LOG_MODE_ON0); status = configSave() ? 200 : 400; } } else if (connData->requestType == HTTPD_METHOD_GET) { diff --git a/esp-link/log.h b/esp-link/log.h index bf2c763..3b7e03a 100644 --- a/esp-link/log.h +++ b/esp-link/log.h @@ -3,9 +3,10 @@ #include "httpd.h" -#define LOG_MODE_AUTO 0 -#define LOG_MODE_OFF 1 -#define LOG_MODE_ON 2 +#define LOG_MODE_AUTO 0 // start by logging to uart0, turn aff after we get an IP +#define LOG_MODE_OFF 1 // always off +#define LOG_MODE_ON0 2 // always log to uart0 +#define LOG_MODE_ON1 3 // always log to uart1 void logInit(void); void log_uart(bool enable); diff --git a/esp-link/main.c b/esp-link/main.c index 8c00bae..1697d2b 100644 --- a/esp-link/main.c +++ b/esp-link/main.c @@ -164,7 +164,7 @@ void user_init(void) { bool restoreOk = configRestore(); // init gpio pin registers gpio_init(); - gpio_output_set(0, 0, 0, (1<<15)); // some people tie it GND, gotta ensure it's disabled + gpio_output_set(0, 0, 0, (1<<15)); // some people tie it to GND, gotta ensure it's disabled // init UART uart_init(flashConfig.baud_rate, 115200); logInit(); // must come after init of uart diff --git a/html/log.html b/html/log.html index 46ec785..cfc4104 100644 --- a/html/log.html +++ b/html/log.html @@ -14,7 +14,8 @@ UART debug log: auto off - on + on uart0 + on uart1


@@ -33,7 +34,7 @@
       fetchText(100, false);
     });
 
-    ["auto", "off", "on"].forEach(function(mode) {
+    ["auto", "off", "on0", "on1"].forEach(function(mode) {
       bnd($('#dbg-'+mode), "click", function(el) {
         ajaxJsonSpin('POST', "/log/dbg?mode="+mode,
           function(data) { showNotification("UART mode " + data.mode); showDbgMode(data.mode); },
diff --git a/serial/uart.c b/serial/uart.c
index ec356d0..5358cf1 100644
--- a/serial/uart.c
+++ b/serial/uart.c
@@ -52,16 +52,13 @@ uart_config(uint8 uart_no)
 {
   if (uart_no == UART1) {
     PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO2_U, FUNC_U1TXD_BK);
-    //PIN_PULLDWN_DIS(PERIPHS_IO_MUX_GPIO2_U);
     PIN_PULLUP_DIS(PERIPHS_IO_MUX_GPIO2_U);
   } else {
     /* rcv_buff size is 0x100 */
     ETS_UART_INTR_ATTACH(uart0_rx_intr_handler,  &(UartDev.rcv_buff));
     PIN_PULLUP_DIS (PERIPHS_IO_MUX_U0TXD_U);
-    //PIN_PULLDWN_DIS(PERIPHS_IO_MUX_U0TXD_U);
     PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0TXD_U, FUNC_U0TXD);
     PIN_PULLUP_DIS (PERIPHS_IO_MUX_U0RXD_U);
-    //PIN_PULLDWN_DIS(PERIPHS_IO_MUX_U0RXD_U);
     PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0RXD_U, 0); // FUNC_U0RXD==0
   }
 

From 6a7d1c8e041271969d838cc6bda0f9c3cc1c8c60 Mon Sep 17 00:00:00 2001
From: Thorsten von Eicken 
Date: Sat, 14 Nov 2015 23:38:05 -0800
Subject: [PATCH 28/43] fix styling of console and make it 100% height

---
 html/console.html | 100 ++++++++---
 html/console.js   |  69 +++-----
 html/style.css    | 442 ++++++++++++++++++++++++++--------------------
 3 files changed, 350 insertions(+), 261 deletions(-)

diff --git a/html/console.html b/html/console.html
index 201f82a..102b85b 100644
--- a/html/console.html
+++ b/html/console.html
@@ -1,33 +1,67 @@
-  
+

Microcontroller Console

-
-

The Microcontroller console shows the last 1024 characters - received from UART0, to which a microcontroller is typically attached. - The UART is configured for 8 bits, no parity, 1 stop bit (8N1).

+

Reset µC -  Baud: - +   Baud: + +   Pgm baud: + +   Fmt: 8N1

-

-      Console entry
-      
-      
-      
-      

Type the command and press ENTER. - Press ESC to clear the entry. - The UP/DOWN arrow keys can be used to get previously sent commands from history.

-
- - +
Console
+
+
+
--- No Content ---
+
+
+
Console entry
+
+ (ENTER to submit, ESC to clear) +
+
+ Add: + + + +
+
+
+
+ + +
+
+
+
History buffer
+
(UP/DOWN arrows to select)
+
-
- - +
+
@@ -37,14 +71,12 @@ diff --git a/html/console.js b/html/console.js index 1080d0a..f29469c 100644 --- a/html/console.js +++ b/html/console.js @@ -1,3 +1,5 @@ +//===== Fetching console text + function fetchText(delay, repeat) { var el = $("#console"); if (el.textEnd == undefined) { @@ -34,37 +36,14 @@ function retryLoad(repeat) { fetchText(1000, repeat); } -//===== Console page - -function showRate(rate) { - rates.forEach(function(r) { - var el = $("#"+r+"-button"); - el.className = el.className.replace(" button-selected", ""); - }); - - var el = $("#"+rate+"-button"); - if (el != null) el.className += " button-selected"; -} - -function baudButton(baud) { - $("#baud-btns").appendChild(m( - ' '+baud+'')); - - $("#"+baud+"-button").addEventListener("click", function(e) { - e.preventDefault(); - ajaxSpin('POST', "/console/baud?rate="+baud, - function(resp) { showNotification("" + baud + " baud set"); showRate(baud); }, - function(s, st) { showWarning("Error setting baud rate: " + st); } - ); - }); -} +//===== Text entry function consoleSendInit() { var sendHistory = $("#send-history"); var inputText = $("#input-text"); var inputAddCr = $("#input-add-cr"); var inputAddLf = $("#input-add-lf"); - + function findHistory(text) { for (var i = 0; i < sendHistory.children.length; i++) { if (text == sendHistory.children[i].value) { @@ -119,28 +98,30 @@ function consoleSendInit() { inputText.addEventListener("keydown", function(e) { switch (e.keyCode) { case 38: /* the up arrow key pressed */ - e.preventDefault(); - navHistory(-1); - break; + e.preventDefault(); + navHistory(-1); + break; case 40: /* the down arrow key pressed */ - e.preventDefault(); - navHistory(+1); - break; + e.preventDefault(); + navHistory(+1); + break; case 27: /* the escape key pressed */ - e.preventDefault(); - inputText.value = ""; - sendHistory.value = ""; - break; + e.preventDefault(); + inputText.value = ""; + sendHistory.value = ""; + break; case 13: /* the enter key pressed */ - e.preventDefault(); - var text = inputText.value; - if (inputAddCr.checked) text += '\r'; - if (inputAddLf.checked) text += '\n'; - ajaxSpin('POST', "/console/send?text=" + encodeURIComponent(text), - function(resp) { showNotification("uC sent"); pushHistory(inputText.value); }, - function(s, st) { showWarning("Error sending text to uC"); } - ); - break; + e.preventDefault(); + var text = inputText.value; + if (inputAddCr.checked) text += '\r'; + if (inputAddLf.checked) text += '\n'; + pushHistory(inputText.value); + inputText.value = ""; + ajaxSpin('POST', "/console/send?text=" + encodeURIComponent(text), + function(resp) { showNotification("Text sent"); }, + function(s, st) { showWarning("Error sending text"); } + ); + break; } }); } diff --git a/html/style.css b/html/style.css index 3ec1100..c612b1f 100644 --- a/html/style.css +++ b/html/style.css @@ -1,10 +1,10 @@ /* All fonts */ html, button, input, select, textarea, .pure-g [class *= "pure-u"] { - font-family: sans-serif; + font-family: sans-serif; } input[type="text"], input[type="password"], textarea { - width: 100%; + width: 100%; } input[type=checkbox] { @@ -16,71 +16,71 @@ body { color: #777; } a:visited, a:link { - color: #009; + color: #009; } a:hover { - color: #00c; + color: #00c; } .card { - background-color: #eee; - padding: 1em; - margin: 0.5em; - -moz-border-radius: 0.5em; - -webkit-border-radius: 0.5em; - border-radius: 0.5em; - border: 0px solid #000000; + background-color: #eee; + padding: 1em; + margin: 0.5em; + -moz-border-radius: 0.5em; + -webkit-border-radius: 0.5em; + border-radius: 0.5em; + border: 0px solid #000000; } /* click-to-edit fields */ .click-to-edit { - position: relative; + position: relative; } .edit-off { - cursor: pointer; + cursor: pointer; } .click-to-edit input, .click-to-edit textarea { - color: black; - background-color: #eee; - width: 100%; + color: black; + background-color: #eee; + width: 100%; } div.edit-on.popup { - position: absolute; - top: 100%; - left: 20px; - background-color: cornsilk; - color: #333; - font-size: 80%; - line-height: 110%; - z-index: 100; - padding: 3px; + position: absolute; + top: 100%; + left: 20px; + background-color: cornsilk; + color: #333; + font-size: 80%; + line-height: 110%; + z-index: 100; + padding: 3px; } /* wifi AP selection form */ #aps label div { - display: inline-block; - margin: 0em 0.2em; + display: inline-block; + margin: 0em 0.2em; } fieldset.radios { - border: none; - padding-left: 0px; + border: none; + padding-left: 0px; } fieldset fields { - clear: both; + clear: both; } #pin-mux input { - display: block; - margin-top: 0.4em; - float: left; + display: block; + margin-top: 0.4em; + float: left; } #pin-mux label { - display: block; - margin: 0em 0.2em 0em 1em; - width: 90%; + display: block; + margin: 0em 0.2em 0em 1em; + width: 90%; } .pure-table td, .pure-table th { - padding: 0.5em 0.5em; + padding: 0.5em 0.5em; } /* make images size-up */ @@ -91,273 +91,337 @@ fieldset fields { /* Add transition to containers so they can push in and out */ #layout, #menu, .menu-link { - -webkit-transition: all 0.2s ease-out; - -moz-transition: all 0.2s ease-out; - -ms-transition: all 0.2s ease-out; - -o-transition: all 0.2s ease-out; - transition: all 0.2s ease-out; + -webkit-transition: all 0.2s ease-out; + -moz-transition: all 0.2s ease-out; + -ms-transition: all 0.2s ease-out; + -o-transition: all 0.2s ease-out; + transition: all 0.2s ease-out; } /* This is the parent `
` that contains the menu and the content area */ #layout { - position: relative; - padding-left: 0; + position: relative; + padding-left: 0; } #layout.active #menu { - left: 150px; - width: 150px; + left: 150px; + width: 150px; } #layout.active .menu-link { - left: 150px; + left: 150px; } div.tt { - font-family: monospace; - font-size: 120%; - color: #390; - background-color: #ddd; - padding: 2px; - margin: 2px 0; - line-height: 100%; + font-family: monospace; + font-size: 120%; + color: #390; + background-color: #ddd; + padding: 2px; + margin: 2px 0; + line-height: 100%; } /* The content `
` */ .content { - margin: 0 auto; - padding: 0 2em; - max-width: 800px; - margin-bottom: 50px; - line-height: 1.6em; + margin: 0 auto; + padding: 0 2em; + max-width: 800px; + margin-bottom: 20px; + line-height: 1.6em; + width: 100%; + box-sizing: border-box; } .header { - margin: 0; - color: #333; - text-align: center; - padding: 2.5em 2em 0; - border-bottom: 1px solid #eee; - background-color: #fc0; + margin: 0; + color: #333; + text-align: center; + padding: 2.5em 2em 0; + border-bottom: 1px solid #eee; + background-color: #fc0; } .header h1 { - margin: 0.2em 0; - font-size: 3em; - font-weight: 300; + margin: 0.2em 0; + font-size: 3em; + font-weight: 300; } .header h1 .esp { - font-size: 1.25em; + font-size: 1.25em; } .jl { - font: normal 800 1.5em sans-serif; - position: relative; - bottom: 19px; - color: #9d1414; - margin-left: 3px; + font: normal 800 1.5em sans-serif; + position: relative; + bottom: 19px; + color: #9d1414; + margin-left: 3px; } .content-subhead { - margin: 50px 0 20px 0; - font-weight: 300; - color: #888; + margin: 50px 0 20px 0; + font-weight: 300; + color: #888; } form button { - margin-top: 0.5em; + margin-top: 0.5em; } .button-primary { - background-color: #99f; + background-color: #99f; } .button-selected { - background-color: #fc6; + background-color: #fc6; +} +select.pure-button { + padding: 0.465em 1em; + color: #009; /* same as a:link */ +} + +input.inline { + float:none; + margin-right: 0px; + margin-left: 0.5em; } /* Text console */ pre.console { - background-color: #663300; - -moz-border-radius: 5px; - -webkit-border-radius: 5px; - border-radius: 5px; - border: 0px solid #000000; - color: #66ff66; - padding: 5px; + background-color: #663300; + -moz-border-radius: 5px; + -webkit-border-radius: 5px; + border-radius: 5px; + border: 0px solid #000000; + color: #66ff66; + padding: 5px; + overflow: scroll; + margin: 0px; } pre.console a { - color: #66ff66; + color: #66ff66; +} + +.console-in { + background-color: #fff0b3; + -moz-border-radius: 5px; + -webkit-border-radius: 5px; + border-radius: 5px; + border: 0px solid #630; + color: #0c0; + padding: 5px; + width: 100%; + height: 100%; + box-sizing: border-box; +} +.console-in option:checked { + background-image: linear-gradient(#fc0, #fc0); +} + +/* console flex */ + +/* +.hbox,.vbox { + box-sizing: border-box; + moz-box-sizing: border-box; + webkit-box-sizing: border-box; +} +*/ +.flex-hbox, .flex-vbox { + display: flex; + display: -ms-flexbox; + display: -webkit-flex; +} +.flex-hbox { + flex-direction: row; + ms-flex-direction: row; + webkit-flex-direction: row; +} +.flex-vbox { + flex-direction: column; + ms-flex-direction: column; + webkit-flex-direction: column; + /*width: 100%; */ +} +.flex-fill { + flex: 1 1 auto; + ms-flex: 1 1 auto; + webkit-flex: 1 1 auto; +} +.height100 { + height: 100%; } /* log page */ .dbg-btn, #refresh-button { - vertical-align: baseline; + vertical-align: baseline; } .lock-icon { - background-image: url("/wifi/icons.png"); - background-color: transparent; - width: 32px; - height: 32px; - display: inline-block; + background-image: url("/wifi/icons.png"); + background-color: transparent; + width: 32px; + height: 32px; + display: inline-block; } #menu { - margin-left: -150px; - width: 150px; - position: fixed; - top: 0; - left: 0; - bottom: 0; - z-index: 1000; - background: #191818; - overflow: visible; - -webkit-overflow-scrolling: touch; + margin-left: -150px; + width: 150px; + position: fixed; + top: 0; + left: 0; + bottom: 0; + z-index: 1000; + background: #191818; + overflow: visible; + -webkit-overflow-scrolling: touch; } #menu a { - color: #999; - border: none; - padding: 0.6em 0 0.6em 0.6em; + color: #999; + border: none; + padding: 0.6em 0 0.6em 0.6em; } #menu .pure-menu, #menu .pure-menu ul { - border: none; - background: transparent; + border: none; + background: transparent; } #menu .pure-menu ul, #menu .pure-menu .menu-item-divided { - border-top: 1px solid #333; + border-top: 1px solid #333; } #menu .pure-menu li a:hover, #menu .pure-menu li a:focus { - background: #333; + background: #333; } #menu .pure-menu-selected, #menu .pure-menu-heading { - background: #9d1414; + background: #9d1414; } #menu .pure-menu-selected a { - color: #fff; + color: #fff; } #menu .pure-menu-heading { - font-size: 110%; - color: #fff; - margin: 0; - text-transform: none; + font-size: 110%; + color: #fff; + margin: 0; + text-transform: none; } #menu .pure-menu-heading img { - vertical-align: middle; - top: -1px; - position: relative; + vertical-align: middle; + top: -1px; + position: relative; } #menu .pure-menu-item { - height:2em; + height:2em; } /* -- Dynamic Button For Responsive Menu -------------------------------------*/ .menu-link { - position: fixed; - display: block; - top: 0; - left: 0; - background: #000; - background: rgba(0,0,0,0.7); - font-size: 10px; - z-index: 10; - width: 2em; - height: auto; - padding: 2.1em 1.6em; + position: fixed; + display: block; + top: 0; + left: 0; + background: #000; + background: rgba(0,0,0,0.7); + font-size: 10px; + z-index: 10; + width: 2em; + height: auto; + padding: 2.1em 1.6em; } .menu-link:hover, .menu-link:focus { - background: #000; + background: #000; } .menu-link span { - position: relative; - display: block; + position: relative; + display: block; } .menu-link span, .menu-link span:before, .menu-link span:after { - background-color: #fff; - width: 100%; - height: 0.2em; + background-color: #fff; + width: 100%; + height: 0.2em; } .menu-link span:before, .menu-link span:after { - position: absolute; - margin-top: -0.6em; - content: " "; + position: absolute; + margin-top: -0.6em; + content: " "; } .menu-link span:after { - margin-top: 0.6em; + margin-top: 0.6em; } /* -- Responsive Styles (Media Queries) ------------------------------------- */ @media (min-width: 56em) { - .header, .content { - padding-left: 2em; - padding-right: 2em; - } - - #layout { - padding-left: 150px; - left: 0; - } - #menu { - left: 150px; - } - - .menu-link { - position: fixed; - left: 150px; - display: none; - } - - #layout.active .menu-link { - left: 150px; - } + .header, .content { + padding-left: 2em; + padding-right: 2em; + } + + #layout { + padding-left: 150px; + left: 0; + } + #menu { + left: 150px; + } + + .menu-link { + position: fixed; + left: 150px; + display: none; + } + + #layout.active .menu-link { + left: 150px; + } } @media (max-width: 56em) { - #layout.active { - position: relative; - left: 150px; - } + #layout.active { + position: relative; + left: 150px; + } } /*===== spinners and notification messages */ #messages { - position: absolute; - left: 25%; - width: 50%; - top: 10; - z-index: 200; - font-size: 110%; - text-align: center; + position: absolute; + left: 25%; + width: 50%; + top: 10; + z-index: 200; + font-size: 110%; + text-align: center; } #warning { - background-color: #933; - color: #fcc; - padding: 0.1em 0.4em; + background-color: #933; + color: #fcc; + padding: 0.1em 0.4em; } #notification { - background-color: #693; - color: #cfc; - padding: 0.1em 0.4em; + background-color: #693; + color: #cfc; + padding: 0.1em 0.4em; } #spinner { - position: absolute; - right: 10%; - top: 20; - z-index: 1000; + position: absolute; + right: 10%; + top: 20; + z-index: 1000; } .spinner { height: 50px; @@ -373,10 +437,10 @@ pre.console a { border-radius: 100%; } .spinner-small { - display: inline-block; - height: 1em; - width: 1em; - border-width: 4px; + display: inline-block; + height: 1em; + width: 1em; + border-width: 4px; } @-webkit-keyframes rotation { From fc8e1cfbf601210b2b6065a9b7ba7dc625d24c32 Mon Sep 17 00:00:00 2001 From: Thorsten von Eicken Date: Sun, 15 Nov 2015 00:06:47 -0800 Subject: [PATCH 29/43] fix scrolling of uC console --- html/console.html | 2 +- html/style.css | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/html/console.html b/html/console.html index 102b85b..a5cb3e8 100644 --- a/html/console.html +++ b/html/console.html @@ -1,4 +1,4 @@ -
+

Microcontroller Console

diff --git a/html/style.css b/html/style.css index c612b1f..99d9688 100644 --- a/html/style.css +++ b/html/style.css @@ -131,6 +131,7 @@ div.tt { line-height: 1.6em; width: 100%; box-sizing: border-box; + overflow: auto; } .header { From 818db2deacc5244fd583d9a0cd82845184d778a0 Mon Sep 17 00:00:00 2001 From: Thorsten von Eicken Date: Sun, 15 Nov 2015 00:20:20 -0800 Subject: [PATCH 30/43] run css through autoprefixer --- html/style.css | 57 ++++++++++++++++++-------------------------------- 1 file changed, 20 insertions(+), 37 deletions(-) diff --git a/html/style.css b/html/style.css index 99d9688..e105354 100644 --- a/html/style.css +++ b/html/style.css @@ -26,8 +26,6 @@ a:hover { background-color: #eee; padding: 1em; margin: 0.5em; - -moz-border-radius: 0.5em; - -webkit-border-radius: 0.5em; border-radius: 0.5em; border: 0px solid #000000; } @@ -92,9 +90,6 @@ fieldset fields { /* Add transition to containers so they can push in and out */ #layout, #menu, .menu-link { -webkit-transition: all 0.2s ease-out; - -moz-transition: all 0.2s ease-out; - -ms-transition: all 0.2s ease-out; - -o-transition: all 0.2s ease-out; transition: all 0.2s ease-out; } @@ -187,8 +182,6 @@ input.inline { /* Text console */ pre.console { background-color: #663300; - -moz-border-radius: 5px; - -webkit-border-radius: 5px; border-radius: 5px; border: 0px solid #000000; color: #66ff66; @@ -203,8 +196,6 @@ pre.console a { .console-in { background-color: #fff0b3; - -moz-border-radius: 5px; - -webkit-border-radius: 5px; border-radius: 5px; border: 0px solid #630; color: #0c0; @@ -214,39 +205,46 @@ pre.console a { box-sizing: border-box; } .console-in option:checked { + background-image: -webkit-linear-gradient(#fc0, #fc0); background-image: linear-gradient(#fc0, #fc0); } /* console flex */ -/* -.hbox,.vbox { - box-sizing: border-box; - moz-box-sizing: border-box; - webkit-box-sizing: border-box; -} -*/ .flex-hbox, .flex-vbox { + display: -webkit-box; display: flex; display: -ms-flexbox; display: -webkit-flex; } .flex-hbox { - flex-direction: row; + -webkit-box-orient: horizontal; + -webkit-box-direction: normal; + -webkit-flex-direction: row; + -ms-flex-direction: row; + flex-direction: row; ms-flex-direction: row; webkit-flex-direction: row; } .flex-vbox { - flex-direction: column; + -webkit-box-orient: vertical; + -webkit-box-direction: normal; + -webkit-flex-direction: column; + -ms-flex-direction: column; + flex-direction: column; ms-flex-direction: column; webkit-flex-direction: column; /*width: 100%; */ } .flex-fill { - flex: 1 1 auto; + -webkit-box-flex: 1; + -webkit-flex: 1 1 auto; + -ms-flex: 1 1 auto; + flex: 1 1 auto; ms-flex: 1 1 auto; webkit-flex: 1 1 auto; } + .height100 { height: 100%; } @@ -428,8 +426,6 @@ pre.console a { height: 50px; width: 50px; -webkit-animation: rotation 1s infinite linear; - -moz-animation: rotation 1s infinite linear; - -o-animation: rotation 1s infinite linear; animation: rotation 1s infinite linear; border-left: 10px solid rgba(204, 51, 0, 0.15); border-right: 10px solid rgba(204, 51, 0, 0.15); @@ -452,27 +448,14 @@ pre.console a { -webkit-transform: rotate(359deg); } } -@-moz-keyframes rotation { - from { - -moz-transform: rotate(0deg); - } - to { - -moz-transform: rotate(359deg); - } -} -@-o-keyframes rotation { - from { - -o-transform: rotate(0deg); - } - to { - -o-transform: rotate(359deg); - } -} + @keyframes rotation { from { + -webkit-transform: rotate(0deg); transform: rotate(0deg); } to { + -webkit-transform: rotate(359deg); transform: rotate(359deg); } } From f0997b13ea7a9d3bb34267475c73f990c4219b42 Mon Sep 17 00:00:00 2001 From: Thorsten von Eicken Date: Sun, 15 Nov 2015 19:14:43 -0800 Subject: [PATCH 31/43] point ot beta5 in readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 57045b4..d91c514 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ Many thanks to https://github.com/brunnels for contributions around the espduino ###[Releases](https://github.com/jeelabs/esp-link/releases) -- [V2.1.beta2](https://github.com/jeelabs/esp-link/releases/tag/v2.1.beta2) has the new built-in +- [V2.1.beta5](https://github.com/jeelabs/esp-link/releases/tag/v2.1.beta5) has the new built-in stk500v1 programmer and works on all modules (esp-01 through esp-12). This is still beta-ware! - [V2.0.rc1](https://github.com/jeelabs/esp-link/releases/tag/v2.0.rc1) has REST support but requires a 1MByte or 4MByte ESP8266 flash, e.g. esp-12 or wroom-02. Despite being labeled From e0040e74bcf7bdadb50b2ac7a2b6ac355deff460 Mon Sep 17 00:00:00 2001 From: Thorsten von Eicken Date: Sun, 15 Nov 2015 19:16:30 -0800 Subject: [PATCH 32/43] refactor pin config, add support for rx pull-up --- esp-link/cgi.c | 23 ++++++- esp-link/cgi.h | 4 ++ esp-link/cgipins.c | 132 ++++++++++++++++++++------------------ esp-link/config.c | 3 +- esp-link/config.h | 5 +- esp-link/main.c | 5 +- html/home.html | 151 ++++++++++++++++++++++++++++++++------------ html/style.css | 79 +++++++++++++++++++++-- html/ui.js | 125 +++++++++++++++++++++++++----------- html/wifi/wifi.html | 8 +-- serial/serbridge.c | 6 +- serial/uart.c | 4 +- 12 files changed, 383 insertions(+), 162 deletions(-) diff --git a/esp-link/cgi.c b/esp-link/cgi.c index f2881cf..ba08c00 100644 --- a/esp-link/cgi.c +++ b/esp-link/cgi.c @@ -59,6 +59,23 @@ getStringArg(HttpdConnData *connData, char *name, char *config, int max_len) { return 1; } +// look for the HTTP arg 'name' and store it at 'config' as an 8-bit integer +// returns -1 on error, 0 if not found, 1 if found and OK +int8_t ICACHE_FLASH_ATTR +getInt8Arg(HttpdConnData *connData, char *name, int8_t *config) { + char buff[16]; + int len = httpdFindArg(connData->getArgs, name, buff, sizeof(buff)); + if (len < 0) return 0; // not found, skip + int m = atoi(buff); + if (len >= 6 || m < -128 || m > 255) { + os_sprintf(buff, "Value for %s out of range", name); + errorResponse(connData, 400, buff); + return -1; + } + *config = m; + return 1; +} + int8_t ICACHE_FLASH_ATTR getBoolArg(HttpdConnData *connData, char *name, bool*config) { char buff[64]; @@ -150,6 +167,10 @@ int ICACHE_FLASH_ATTR cgiMenu(HttpdConnData *connData) { httpdHeader(connData, "Cache-Control", "max-age=3600, must-revalidate"); httpdHeader(connData, "Content-Type", "application/json"); httpdEndHeaders(connData); + // limit hostname to 12 chars + char name[13]; + os_strncpy(name, flashConfig.hostname, 12); + name[12] = 0; // construct json response os_sprintf(buff, "{\"menu\": [\"Home\", \"/home.html\", " @@ -160,7 +181,7 @@ int ICACHE_FLASH_ATTR cgiMenu(HttpdConnData *connData) { #endif "\"Debug log\", \"/log.html\" ],\n" " \"version\": \"%s\"," - "\"name\":\"%s\"}", esp_link_version, flashConfig.sys_name); + "\"name\":\"%s\"}", esp_link_version, name); httpdSend(connData, buff, -1); return HTTPD_CGI_DONE; } diff --git a/esp-link/cgi.h b/esp-link/cgi.h index 2b8893c..e64b301 100644 --- a/esp-link/cgi.h +++ b/esp-link/cgi.h @@ -12,6 +12,10 @@ void errorResponse(HttpdConnData *connData, int code, char *message); // 'max_len' (incl terminating zero), returns -1 on error, 0 if not found, 1 if found int8_t getStringArg(HttpdConnData *connData, char *name, char *config, int max_len); +// Get the HTTP query-string param 'name' and store it as a int8 value at 'config', +// supports signed and unsigned, returns -1 on error, 0 if not found, 1 if found +int8_t getInt8Arg(HttpdConnData *connData, char *name, int8_t *config); + // Get the HTTP query-string param 'name' and store it boolean value at 'config', // supports 1/true and 0/false, returns -1 on error, 0 if not found, 1 if found int8_t getBoolArg(HttpdConnData *connData, char *name, bool*config); diff --git a/esp-link/cgipins.c b/esp-link/cgipins.c index da4a8d1..913d9fa 100644 --- a/esp-link/cgipins.c +++ b/esp-link/cgipins.c @@ -7,6 +7,7 @@ #include "status.h" #include "serbridge.h" +#if 0 static char *map_names[] = { "esp-bridge", "jn-esp-v2", "esp-01(AVR)", "esp-01(ARM)", "esp-br-rev", "wifi-link-12", }; @@ -21,46 +22,19 @@ static int8_t map_asn[][5] = { }; static const int num_map_names = sizeof(map_names)/sizeof(char*); static const int num_map_func = sizeof(map_func)/sizeof(char*); +#endif // Cgi to return choice of pin assignments int ICACHE_FLASH_ATTR cgiPinsGet(HttpdConnData *connData) { if (connData->conn==NULL) return HTTPD_CGI_DONE; // Connection aborted - char buff[2048]; + char buff[1024]; int len; - // figure out current mapping - int curr = 0; - for (int i=0; i= 0) - len += os_sprintf(buff+len, " %s:gpio%d", map_func[f], p); - else - len += os_sprintf(buff+len, " %s:n/a", map_func[f]); - } - len += os_sprintf(buff+len, "\" }"); - } - len += os_sprintf(buff+len, "\n] }"); + len = os_sprintf(buff, + "{ \"reset\":%d, \"isp\":%d, \"conn\":%d, \"ser\":%d, \"swap\":%d, \"rxpup\":%d }", + flashConfig.reset_pin, flashConfig.isp_pin, flashConfig.conn_led_pin, + flashConfig.ser_led_pin, !!flashConfig.swap_uart, 1); jsonHeader(connData, 200); httpdSend(connData, buff, len); @@ -73,41 +47,73 @@ int ICACHE_FLASH_ATTR cgiPinsSet(HttpdConnData *connData) { return HTTPD_CGI_DONE; // Connection aborted } - char buff[128]; - int len = httpdFindArg(connData->getArgs, "map", buff, sizeof(buff)); - if (len <= 0) { - jsonHeader(connData, 400); - return HTTPD_CGI_DONE; - } + int8_t ok = 0; + int8_t reset, isp, conn, ser; + bool swap, rxpup; + ok |= getInt8Arg(connData, "reset", &reset); + ok |= getInt8Arg(connData, "isp", &isp); + ok |= getInt8Arg(connData, "conn", &conn); + ok |= getInt8Arg(connData, "ser", &ser); + ok |= getBoolArg(connData, "swap", &swap); + ok |= getBoolArg(connData, "rxpup", &rxpup); + if (ok < 0) return HTTPD_CGI_DONE; - int m = atoi(buff); - if (m < 0 || m >= num_map_names) { - jsonHeader(connData, 400); - return HTTPD_CGI_DONE; - } -#ifdef CGIPINS_DBG - os_printf("Switching pin map to %s (%d)\n", map_names[m], m); -#endif - int8_t *map = map_asn[m]; - flashConfig.reset_pin = map[0]; - flashConfig.isp_pin = map[1]; - flashConfig.conn_led_pin = map[2]; - flashConfig.ser_led_pin = map[3]; - flashConfig.swap_uart = map[4]; + char *coll; + if (ok > 0) { + // check whether two pins collide + uint16_t pins = 0; + if (reset >= 0) pins = 1 << reset; + if (isp >= 0) { + if (pins & (1<= 0) { + if (pins & (1<= 0) { + if (pins & (1<reason, rst_codes[rst_info->reason], + flashConfig.hostname, rst_info->reason, rst_codes[rst_info->reason], flash_maps[system_get_flash_size_map()], fid & 0xff, (fid&0xff00)|((fid>>16)&0xff), part_id ? "user2.bin" : "user1.bin", flashConfig.slip_enable ? "enabled" : "disabled", @@ -134,7 +134,7 @@ static int ICACHE_FLASH_ATTR cgiSystemSet(HttpdConnData *connData) { if (connData->conn==NULL) return HTTPD_CGI_DONE; // Connection aborted. Clean up. int8_t status = 0; - status |= getStringArg(connData, "name", flashConfig.sys_name, sizeof(flashConfig.sys_name)); + status |= getStringArg(connData, "name", flashConfig.hostname, sizeof(flashConfig.hostname)); status |= getStringArg(connData, "description", flashConfig.sys_descr, sizeof(flashConfig.sys_descr)); if (status < 0) return HTTPD_CGI_DONE; // getStringArg has produced an error response @@ -172,7 +172,6 @@ void user_init(void) { os_delay_us(10000L); os_printf("\n\n** %s\n", esp_link_version); os_printf("Flash config restore %s\n", restoreOk ? "ok" : "*FAILED*"); - if (flashConfig.sys_name[0] == 0) os_strcpy(flashConfig.sys_name, "name-me"); #if defined(STA_SSID) && defined(STA_PASS) int x = wifi_get_opmode() & 0x3; diff --git a/html/home.html b/html/home.html index 69dccff..2e0e0bc 100644 --- a/html/home.html +++ b/html/home.html @@ -7,65 +7,132 @@
-
-

The JeeLabs esp-link firmware bridges the ESP8266 - serial port to Wifi and can - program microcontrollers over the serial port, in particular Arduinos, AVRs, and - NXP's LPC800 and other ARM processors. Typical avrdude command line to - program an Arduino:

-
/home/arduino/hardware/tools/avrdude \
-   -DV -patmega328p -Pnet:esp-link.local:23 -carduino -b115200 -U \
-   -C /home/arduino/hardware/tools/avrdude.conf flash:w:my_sketch.hex:i -
-

where -Pnet:esp-link.local:23 tells avrdude to connect to port 23 of esp-link. - You can substitute the IP address of your esp-link for esp-link.local if necessary. - Please refer to - the online README - for up-to-date help.

-
-
-
+
-

Wifi summary

+

System overview

- - - - - - + + + + + + +
-

Esp-link summary

+

Info

+

The JeeLabs esp-link firmware bridges the ESP8266 + serial port to Wifi and can + program microcontrollers over the serial port, in particular Arduinos, AVRs, and + NXP's LPC800 and other ARM processors. Typical avrdude command line to + program an Arduino:

+
/home/arduino/hardware/tools/avrdude \
+   -DV -patmega328p \
+   -Pnet:esp-link.local:23 \
+   -carduino -b115200 -U \
+   -C /home/arduino/hardware/tools/avrdude.conf \
+   flash:w:my_sketch.hex:i +
+

where -Pnet:esp-link.local:23 tells avrdude to connect to port 23 of esp-link. + You can substitute the IP address of your esp-link for esp-link.local if necessary. + Please refer to + the online README + for up-to-date help.

+
+
+ +
+
+

Pin assignment

+
+ +
+
+

System details

- - + + + + - - - -
-
-

Pin assignment

- Select one of the following signal/pin assignments to match your hardware -
-
-
-
@@ -76,9 +143,11 @@ diff --git a/html/style.css b/html/style.css index e105354..d3f9348 100644 --- a/html/style.css +++ b/html/style.css @@ -42,16 +42,71 @@ a:hover { background-color: #eee; width: 100%; } -div.edit-on.popup { +.click-to-edit span, .click-to-edit div { + /*background-color: #e0e0e0;*/ + /*color: #1c0099;*/ + padding: 0.3em; + width: 100%; + color: #444; /* rgba not supported (IE 8) */ + color: rgba(0, 0, 0, 0.80); /* rgba supported */ + border: 1px solid #999; /*IE 6/7/8*/ + border: none rgba(0, 0, 0, 0); /*IE9 + everything else*/ + background-color: #E6E6E6; + text-decoration: none; + border-radius: 2px; +} +.click-to-edit span:hover, .click-to-edit div:hover, +.click-to-edit span:focus, .click-to-edit div:focus { + filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#00000000', endColorstr='#1a000000',GradientType=0); + background-image: -webkit-gradient(linear, 0 0, 0 100%, from(transparent), color-stop(40%, rgba(0,0,0, 0.05)), to(rgba(0,0,0, 0.10))); + background-image: -webkit-linear-gradient(transparent, rgba(0,0,0, 0.05) 40%, rgba(0,0,0, 0.10)); + background-image: -moz-linear-gradient(top, rgba(0,0,0, 0.05) 0%, rgba(0,0,0, 0.10)); + background-image: -o-linear-gradient(transparent, rgba(0,0,0, 0.05) 40%, rgba(0,0,0, 0.10)); + background-image: linear-gradient(transparent, rgba(0,0,0, 0.05) 40%, rgba(0,0,0, 0.10)); +} +.click-to-edit span:focus, .click-to-edit div:focus { + outline: 0; +} +.click-to-edit span:active, .click-to-edit div:active { + box-shadow: 0 0 0 1px rgba(0,0,0, 0.15) inset, 0 0 6px rgba(0,0,0, 0.20) inset; + border-color: #000\9; +} +/* Firefox: Get rid of the inner focus border */ +.click-to-edit span::-moz-focus-inner, +.click-to-edit div::-moz-focus-inner { + padding: 0; + border: 0; +} + +/* pop-ups */ +.popup-hidden { + display: none; +} +.popup, div.popup { position: absolute; - top: 100%; - left: 20px; - background-color: cornsilk; + /*top: 100%;*/ + bottom: 100%; + background-color: #fff0b3; + border-radius: 5px; + border: 0px solid #000; color: #333; font-size: 80%; line-height: 110%; z-index: 100; - padding: 3px; + padding: 5px; + min-width: 20em; +} +.popup:not(.pop-left) { + left: 20px; +} +.popup.pop-left { + right: 20px; +} +.popup-target:hover .popup { + display: block; +} +.popup-target { + position: relative; } /* wifi AP selection form */ @@ -81,6 +136,16 @@ fieldset fields { padding: 0.5em 0.5em; } +/* Narrow left-aligned Forms */ +.pure-form-aligned.form-narrow .pure-control-group label { + text-align: left; + width: 6em; +} +.pure-form-aligned.form-narrow input[type=checkbox], +.pure-form-aligned.form-narrow input[type=radio] { + float: none; +} + /* make images size-up */ .xx-pure-img-responsive { max-width: 100%; @@ -172,6 +237,10 @@ select.pure-button { padding: 0.465em 1em; color: #009; /* same as a:link */ } +.button-small { + font-size: 75%; + background: #ccc; +} input.inline { float:none; diff --git a/html/ui.js b/html/ui.js index 8268385..a9bef1d 100644 --- a/html/ui.js +++ b/html/ui.js @@ -213,6 +213,11 @@ function ajaxJsonSpin(method, url, ok_cb, err_cb) { //===== main menu, header spinner and notification boxes +function hidePopup(el) { + addClass(el, "popup-hidden"); + addClass(el.parentNode, "popup-target"); +} + onLoad(function() { var l = $("#layout"); var o = l.childNodes[0]; @@ -229,11 +234,7 @@ onLoad(function() {
\ \  esp-link\ -
\ - \ - \ - \ -
\ +
\ \
\
\ @@ -251,6 +252,11 @@ onLoad(function() { toggleClass(ml, active); }); + // hide pop-ups + domForEach($(".popup"), function(el) { + hidePopup(el); + }); + // populate menu via ajax call var getMenu = function() { ajaxJson("GET", "/menu", function(data) { @@ -267,8 +273,7 @@ onLoad(function() { var v = $("#version"); if (v != null) { v.innerHTML = data.version; } - setEditToClick("system-name", data.name); - makeAjaxInput("system", "name"); + setEditToClick("system-name", data["name"]); }, function() { setTimeout(getMenu, 1000); }); }; getMenu(); @@ -313,7 +318,7 @@ function setEditToClick(klass, value) { function showSystemInfo(data) { Object.keys(data).forEach(function(v) { - setEditToClick("system-" + v, data[v]); + setEditToClick("system-"+v, data[v]); }); $("#system-spinner").setAttribute("hidden", ""); $("#system-table").removeAttribute("hidden"); @@ -331,6 +336,8 @@ function makeAjaxInput(klass, field) { var eoff = $(".edit-off", div)[0]; var url = "/"+klass+"/update?"+field; + if (eoff === undefined || eon == undefined) return; + var enableEditToClick = function() { eoff.setAttribute('hidden',''); domForEach(eon, function(el){ el.removeAttribute('hidden'); }); @@ -366,6 +373,7 @@ function showWarning(text) { var el = $("#warning"); el.innerHTML = text; el.removeAttribute('hidden'); + window.scrollTo(0, 0); } function hideWarning() { el = $("#warning").setAttribute('hidden', ''); @@ -384,36 +392,70 @@ function showNotification(text) { //===== GPIO Pin mux card -var currPin; -// pin={reset:12, isp:13, LED_conn:0, LED_ser:2} -function createInputForPin(pin) { - var input = document.createElement("input"); - input.type = "radio"; - input.name = "pins"; - input.data = pin.name; - input.className = "pin-input"; - input.value= pin.value; - input.id = "opt-" + pin.value; - if (currPin == pin.name) input.checked = "1"; - - var descr = m('"); - var div = document.createElement("div"); - div.appendChild(input); - div.appendChild(descr); - return div; +var pinPresets = { + // array: reset, isp, conn, ser, swap, rxpup + "esp-01": [ 0, -1, 2, -1, 0, 1 ], + "esp-12": [ 12, 14, 0, 2, 0, 1 ], + "esp-12 swap": [ 1, 3, 0, 2, 1, 1 ], + "esp-bridge": [ 12, 13, 0, 14, 0, 0 ], + "wifi-link-12": [ 1, 3, 0, 2, 1, 0 ], +}; + +function createPresets(sel) { + for (var p in pinPresets) { + var opt = m(''); + sel.appendChild(opt); + } + + function applyPreset(v) { + var pp = pinPresets[v]; + if (pp === undefined) return pp; + console.log("apply preset:", v, pp); + function setPP(k, v) { $("#pin-"+k).value = v; }; + setPP("reset", pp[0]); + setPP("isp", pp[1]); + setPP("conn", pp[2]); + setPP("ser", pp[3]); + setPP("swap", pp[4]); + $("#pin-rxpup").checked = !!pp[5]; + }; + + bnd(sel, "change", function(ev) { + console.log("preset change:", sel.value); + ev.preventDefault(); + applyPreset(sel.value); + }); } function displayPins(resp) { - var po = $("#pin-mux"); - po.innerHTML = ""; - currPin = resp.curr; - resp.map.forEach(function(v) { - po.appendChild(createInputForPin(v)); - }); - var i, inputs = $(".pin-input"); - for (i=0; i= 0) opt.innerHTML = "gpio"+i; + else opt.innerHTML = "disabled"; + if (i===1) opt.innerHTML += "/TX0"; + if (i===2) opt.innerHTML += "/TX1"; + if (i===3) opt.innerHTML += "/RX0"; + if (i==v) opt.selected = true; + sel.appendChild(opt); + }); + var pup = $(".popup", sel.parentNode); + if (pup !== undefined) hidePopup(pup[0]); }; + + createSelectForPin("reset", resp["reset"]); + createSelectForPin("isp", resp["isp"]); + createSelectForPin("conn", resp["conn"]); + createSelectForPin("ser", resp["ser"]); + $("#pin-swap").value = resp["swap"]; + $("#pin-rxpup").checked = !!resp["rxpup"]; + createPresets($("#pin-preset")); + + $("#pin-spinner").setAttribute("hidden", ""); + $("#pin-table").removeAttribute("hidden"); } function fetchPins() { @@ -423,10 +465,17 @@ function fetchPins() { } function setPins(v, name) { - ajaxSpin("POST", "/pins?map="+v, function() { - showNotification("Pin assignment changed to " + name); - }, function() { - showNotification("Pin assignment change failed"); + var url = "/pins"; + var sep = "?"; + ["reset", "isp", "conn", "ser", "swap"].forEach(function(p) { + url += sep + p + "=" + $("#pin-"+p).value; + sep = "&"; + }); + url += "&rxpup=" + ($("#pin-rxpup").selected ? "1" : "0"); + ajaxSpin("POST", url, function() { + showNotification("Pin assignment changed"); + }, function(status, errMsg) { + showWarning(errMsg); window.setTimeout(fetchPins, 100); }); } diff --git a/html/wifi/wifi.html b/html/wifi/wifi.html index 4ddfc20..200cf53 100644 --- a/html/wifi/wifi.html +++ b/html/wifi/wifi.html @@ -30,7 +30,8 @@
Scanning...
@@ -51,10 +52,7 @@ Static IP
-
- - -
+
diff --git a/serial/serbridge.c b/serial/serbridge.c index da8835a..c996b9a 100644 --- a/serial/serbridge.c +++ b/serial/serbridge.c @@ -446,11 +446,15 @@ serbridgeInitPins() PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTCK_U, 4); PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDO_U, 4); PIN_PULLUP_DIS(PERIPHS_IO_MUX_MTCK_U); - PIN_PULLUP_DIS(PERIPHS_IO_MUX_MTDO_U); + if (flashConfig.rx_pullup) PIN_PULLUP_EN(PERIPHS_IO_MUX_MTDO_U); + else PIN_PULLUP_DIS(PERIPHS_IO_MUX_MTDO_U); system_uart_swap(); } else { PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0TXD_U, 0); PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0RXD_U, 0); + PIN_PULLUP_DIS(PERIPHS_IO_MUX_U0TXD_U); + if (flashConfig.rx_pullup) PIN_PULLUP_EN(PERIPHS_IO_MUX_U0RXD_U); + else PIN_PULLUP_DIS(PERIPHS_IO_MUX_U0RXD_U); system_uart_de_swap(); } diff --git a/serial/uart.c b/serial/uart.c index 5358cf1..8e61f7e 100644 --- a/serial/uart.c +++ b/serial/uart.c @@ -56,10 +56,10 @@ uart_config(uint8 uart_no) } else { /* rcv_buff size is 0x100 */ ETS_UART_INTR_ATTACH(uart0_rx_intr_handler, &(UartDev.rcv_buff)); - PIN_PULLUP_DIS (PERIPHS_IO_MUX_U0TXD_U); PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0TXD_U, FUNC_U0TXD); - PIN_PULLUP_DIS (PERIPHS_IO_MUX_U0RXD_U); PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0RXD_U, 0); // FUNC_U0RXD==0 + //PIN_PULLUP_DIS (PERIPHS_IO_MUX_U0TXD_U); now done in serbridgeInitPins + //PIN_PULLUP_DIS (PERIPHS_IO_MUX_U0RXD_U); } uart_div_modify(uart_no, UART_CLK_FREQ / UartDev.baut_rate); From 6b604ffc7ca355e644e6e497d8c0ae9e0fc5f7c0 Mon Sep 17 00:00:00 2001 From: Thorsten von Eicken Date: Sun, 15 Nov 2015 19:16:51 -0800 Subject: [PATCH 33/43] remove pgm baud rate UI for now --- html/console.html | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/html/console.html b/html/console.html index a5cb3e8..e638af4 100644 --- a/html/console.html +++ b/html/console.html @@ -17,6 +17,7 @@ +   Fmt: 8N1

From d156618a684532488fbb3f6202c2e54ea904f639 Mon Sep 17 00:00:00 2001 From: Thorsten von Eicken Date: Sun, 15 Nov 2015 22:07:09 -0800 Subject: [PATCH 34/43] make sure optiboot frees uart CB when done --- esp-link/cgioptiboot.c | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/esp-link/cgioptiboot.c b/esp-link/cgioptiboot.c index bd2dd2a..73bf908 100644 --- a/esp-link/cgioptiboot.c +++ b/esp-link/cgioptiboot.c @@ -21,6 +21,8 @@ #define DBG(format, ...) do { } while(0) #endif +#define DBG_GPIO5 0 // define to 1 to use GPIO5 to trigger scope + //===== global state static ETSTimer optibootTimer; @@ -71,7 +73,7 @@ static void ICACHE_FLASH_ATTR optibootInit() { ackWait = 0; errMessage[0] = 0; responseLen = 0; - programmingCB = optibootUartRecv; + programmingCB = NULL; if (optibootData != NULL) { if (optibootData->conn != NULL) optibootData->conn->cgiPrivData = (void *)-1; // signal that request has been aborted @@ -91,7 +93,9 @@ void ICACHE_FLASH_ATTR appendPretty(char *buf, char *raw, int max) { int i = 0; while (off < max-5) { unsigned char c = raw[i++]; - if (c >= ' ' && c <= '~') { + if (c == 0) { + break; + } else if (c >= ' ' && c <= '~') { buf[off++] = c; } else if (c == '\n') { buf[off++] = '\\'; @@ -103,7 +107,7 @@ void ICACHE_FLASH_ATTR appendPretty(char *buf, char *raw, int max) { buf[off++] = '\\'; buf[off++] = 'x'; buf[off++] = '0'+(unsigned char)((c>>4)+((c>>4)>9?7:0)); - buf[off++] = '0'+(unsigned char)((c&0xff)+((c&0xff)>9?7:0)); + buf[off++] = '0'+(unsigned char)((c&0xf)+((c&0xf)>9?7:0)); } } buf[off] = 0; @@ -120,9 +124,12 @@ int ICACHE_FLASH_ATTR cgiOptibootSync(HttpdConnData *connData) { } else if (connData->requestType == HTTPD_METHOD_POST) { // issue reset optibootInit(); + programmingCB = optibootUartRecv; serbridgeReset(); +#if DBG_GPIO5 makeGpio(5); gpio_output_set(0, (1<<5), (1<<5), 0); // output 0 +#endif // start sync timer os_timer_disarm(&optibootTimer); @@ -394,7 +401,9 @@ static bool pollAck() { char recv[16]; uint16_t need = ackWait*2; uint16_t got = uart0_rx_poll(recv, need, 50000); +#ifdef DBG_GPIO5 gpio_output_set(0, (1<<5), (1<<5), 0); // output 0 +#endif if (got < need) { os_strcpy(errMessage, "Timeout waiting for flash page to be programmed"); return false; @@ -421,7 +430,9 @@ static bool ICACHE_FLASH_ATTR programPage(void) { DBG("OB pgm %d@0x%lx ackWait=%d\n", pgmLen, optibootData->address, ackWait); // send address to optiboot (little endian format) +#ifdef DBG_GPIO5 gpio_output_set((1<<5), 0, (1<<5), 0); // output 1 +#endif ackWait++; uart0_write_char(STK_LOAD_ADDRESS); uint16_t addr = optibootData->address >> 1; // word address @@ -436,7 +447,9 @@ static bool ICACHE_FLASH_ATTR programPage(void) { armTimer(); // send page length (big-endian format, go figure...) +#ifdef DBG_GPIO5 gpio_output_set((1<<5), 0, (1<<5), 0); // output 1 +#endif ackWait++; uart0_write_char(STK_PROG_PAGE); uart0_write_char(pgmLen>>8); @@ -520,10 +533,10 @@ static short ICACHE_FLASH_ATTR skipInSync(char *buf, short length) { static void ICACHE_FLASH_ATTR optibootUartRecv(char *buf, short length) { // append what we got to what we have accumulated if (responseLen < RESP_SZ-1) { - short cpy = RESP_SZ-1-responseLen; - if (cpy > length) cpy = length; - os_memcpy(responseBuf+responseLen, buf, cpy); - responseLen += cpy; + char *rb = responseBuf+responseLen; + for (short i=0; i Date: Sun, 15 Nov 2015 22:07:36 -0800 Subject: [PATCH 35/43] fix pin change collision detection --- esp-link/cgipins.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/esp-link/cgipins.c b/esp-link/cgipins.c index 913d9fa..8455a16 100644 --- a/esp-link/cgipins.c +++ b/esp-link/cgipins.c @@ -76,11 +76,11 @@ int ICACHE_FLASH_ATTR cgiPinsSet(HttpdConnData *connData) { pins |= 1 << ser; } if (swap) { - if (pins & (1<<1)) { coll = "Uart TX"; goto collision; } - if (pins & (1<<3)) { coll = "Uart RX"; goto collision; } - } else { if (pins & (1<<15)) { coll = "Uart TX"; goto collision; } if (pins & (1<<13)) { coll = "Uart RX"; goto collision; } + } else { + if (pins & (1<<1)) { coll = "Uart TX"; goto collision; } + if (pins & (1<<3)) { coll = "Uart RX"; goto collision; } } // we're good, set flashconfig @@ -90,6 +90,8 @@ int ICACHE_FLASH_ATTR cgiPinsSet(HttpdConnData *connData) { flashConfig.ser_led_pin = ser; flashConfig.swap_uart = swap; flashConfig.rx_pullup = rxpup; + os_printf("Pins changed: reset=%d isp=%d conn=%d ser=%d swap=%d rx-pup=%d\n", + reset, isp, conn, ser, swap, rxpup); // apply the changes serbridgeInitPins(); From ce05e00bd7cad47b47ef120a8fcff0db385fa3d9 Mon Sep 17 00:00:00 2001 From: Thorsten von Eicken Date: Sun, 15 Nov 2015 22:08:32 -0800 Subject: [PATCH 36/43] fix pin changes and hostname change --- esp-link/cgiwifi.c | 2 +- esp-link/cgiwifi.h | 1 + esp-link/log.c | 2 +- esp-link/main.c | 16 ++++++++++++---- html/ui.js | 7 +++++-- 5 files changed, 20 insertions(+), 8 deletions(-) diff --git a/esp-link/cgiwifi.c b/esp-link/cgiwifi.c index e379b12..a56eee2 100644 --- a/esp-link/cgiwifi.c +++ b/esp-link/cgiwifi.c @@ -403,7 +403,7 @@ static void ICACHE_FLASH_ATTR debugIP() { #endif // configure Wifi, specifically DHCP vs static IP address based on flash config -static void ICACHE_FLASH_ATTR configWifiIP() { +void ICACHE_FLASH_ATTR configWifiIP() { if (flashConfig.staticip == 0) { // let's DHCP! wifi_station_set_hostname(flashConfig.hostname); diff --git a/esp-link/cgiwifi.h b/esp-link/cgiwifi.h index a2782a5..37a02d6 100644 --- a/esp-link/cgiwifi.h +++ b/esp-link/cgiwifi.h @@ -13,6 +13,7 @@ int cgiWiFiConnect(HttpdConnData *connData); int cgiWiFiSetMode(HttpdConnData *connData); int cgiWiFiConnStatus(HttpdConnData *connData); int cgiWiFiSpecial(HttpdConnData *connData); +void configWifiIP(); void wifiInit(void); void wifiAddStateChangeCb(WifiStateChangeCb cb); diff --git a/esp-link/log.c b/esp-link/log.c index d7035c0..d6c245b 100644 --- a/esp-link/log.c +++ b/esp-link/log.c @@ -128,7 +128,7 @@ ajaxLog(HttpdConnData *connData) { return HTTPD_CGI_DONE; } -static char *dbg_mode[] = { "auto", "off", "on u0", "on u1" }; +static char *dbg_mode[] = { "auto", "off", "on0", "on1" }; int ICACHE_FLASH_ATTR ajaxLogDbg(HttpdConnData *connData) { diff --git a/esp-link/main.c b/esp-link/main.c index 46cc080..3719d9c 100644 --- a/esp-link/main.c +++ b/esp-link/main.c @@ -129,14 +129,22 @@ static int ICACHE_FLASH_ATTR cgiSystemInfo(HttpdConnData *connData) { return HTTPD_CGI_DONE; } +static ETSTimer reassTimer; + // Cgi to update system info (name/description) static int ICACHE_FLASH_ATTR cgiSystemSet(HttpdConnData *connData) { if (connData->conn==NULL) return HTTPD_CGI_DONE; // Connection aborted. Clean up. - int8_t status = 0; - status |= getStringArg(connData, "name", flashConfig.hostname, sizeof(flashConfig.hostname)); - status |= getStringArg(connData, "description", flashConfig.sys_descr, sizeof(flashConfig.sys_descr)); - if (status < 0) return HTTPD_CGI_DONE; // getStringArg has produced an error response + int8_t n = getStringArg(connData, "name", flashConfig.hostname, sizeof(flashConfig.hostname)); + int8_t d = getStringArg(connData, "description", flashConfig.sys_descr, sizeof(flashConfig.sys_descr)); + if (n < 0 || d < 0) return HTTPD_CGI_DONE; // getStringArg has produced an error response + + if (n > 0) { + // schedule hostname change-over + os_timer_disarm(&reassTimer); + os_timer_setfn(&reassTimer, configWifiIP, NULL); + os_timer_arm(&reassTimer, 1000, 0); // 1 second for the response of this request to make it + } if (configSave()) { httpdStartResponse(connData, 204); diff --git a/html/ui.js b/html/ui.js index a9bef1d..cbf3615 100644 --- a/html/ui.js +++ b/html/ui.js @@ -418,10 +418,10 @@ function createPresets(sel) { setPP("ser", pp[3]); setPP("swap", pp[4]); $("#pin-rxpup").checked = !!pp[5]; + sel.value = 0; }; bnd(sel, "change", function(ev) { - console.log("preset change:", sel.value); ev.preventDefault(); applyPreset(sel.value); }); @@ -431,6 +431,7 @@ function displayPins(resp) { function createSelectForPin(name, v) { var sel = $("#pin-"+name); addClass(sel, "pure-button"); + sel.innerHTML = ""; [-1,0,1,2,3,4,5,12,13,14,15].forEach(function(i) { var opt = document.createElement("option"); opt.value = i; @@ -464,7 +465,8 @@ function fetchPins() { }); } -function setPins(v, name) { +function setPins(ev) { + ev.preventDefault(); var url = "/pins"; var sep = "?"; ["reset", "isp", "conn", "ser", "swap"].forEach(function(p) { @@ -472,6 +474,7 @@ function setPins(v, name) { sep = "&"; }); url += "&rxpup=" + ($("#pin-rxpup").selected ? "1" : "0"); + console.log("set pins: " + url); ajaxSpin("POST", url, function() { showNotification("Pin assignment changed"); }, function(status, errMsg) { From c3e1a8a83bc4ec6e346ac651d5d816458da457c8 Mon Sep 17 00:00:00 2001 From: Thorsten von Eicken Date: Sun, 15 Nov 2015 22:08:43 -0800 Subject: [PATCH 37/43] improve home page --- html/console.js | 9 +++++++++ html/home.html | 13 +++++++------ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/html/console.js b/html/console.js index f29469c..e85d6d7 100644 --- a/html/console.js +++ b/html/console.js @@ -22,12 +22,21 @@ function updateText(resp) { var delay = 3000; if (resp != null && resp.len > 0) { console.log("updateText got", resp.len, "chars at", resp.start); + var isScrolledToBottom = el.scrollHeight - el.clientHeight <= el.scrollTop + 1; + //console.log("isScrolledToBottom="+isScrolledToBottom, "scrollHeight="+el.scrollHeight, + // "clientHeight="+el.clientHeight, "scrollTop="+el.scrollTop, + // "" + (el.scrollHeight - el.clientHeight) + "<=" + (el.scrollTop + 1)); + + // append the text if (resp.start > el.textEnd) { el.innerHTML = el.innerHTML.concat("\r\n - @@ -36,11 +36,12 @@ program microcontrollers over the serial port, in particular Arduinos, AVRs, and NXP's LPC800 and other ARM processors. Typical avrdude command line to program an Arduino:

-
/home/arduino/hardware/tools/avrdude \
+
+ /home/arduino/hardware/tools/avrdude \
  -DV -patmega328p \
  -Pnet:esp-link.local:23 \
-   -carduino -b115200 -U \
-   -C /home/arduino/hardware/tools/avrdude.conf \
+   -carduino -b115200 -U -C \
+   /home/arduino/hardware/tools/avrdude.conf \
  flash:w:my_sketch.hex:i

where -Pnet:esp-link.local:23 tells avrdude to connect to port 23 of esp-link. @@ -125,8 +126,8 @@ Description:

- - From 4e100b01426243c4d44e782ed814ba033290dd2f Mon Sep 17 00:00:00 2001 From: Thorsten von Eicken Date: Sun, 15 Nov 2015 23:55:49 -0800 Subject: [PATCH 38/43] optiboot auto-switch baud rate --- esp-link/cgioptiboot.c | 82 ++++++++++++++++++++++++++++-------------- 1 file changed, 55 insertions(+), 27 deletions(-) diff --git a/esp-link/cgioptiboot.c b/esp-link/cgioptiboot.c index 73bf908..47cdea2 100644 --- a/esp-link/cgioptiboot.c +++ b/esp-link/cgioptiboot.c @@ -10,8 +10,11 @@ #include "serbridge.h" #include "serled.h" -#define SYNC_TIMEOUT (2000) // to achieve sync, in milliseconds -#define PGM_TIMEOUT (20000) // when sync is achieved, in milliseconds +#define SYNC_TIMEOUT 3600 // to achieve sync on initial baud rate, in milliseconds +#define SYNC_INTERVAL 25 // interval at which we try to sync +#define BAUD_INTERVAL 400 // interval after which we change baud rate +#define PGM_TIMEOUT 20000 // timeout when sync is achieved, in milliseconds +#define PGM_INTERVAL 200 // send sync at this interval in ms when in programming mode #define OPTIBOOT_DBG #undef DBG @@ -21,7 +24,7 @@ #define DBG(format, ...) do { } while(0) #endif -#define DBG_GPIO5 0 // define to 1 to use GPIO5 to trigger scope +#define DBG_GPIO5 1 // define to 1 to use GPIO5 to trigger scope //===== global state @@ -35,8 +38,10 @@ static enum { // overall programming states stateProg, // programming... } progState; static short syncCnt; // counter & timeout for sync attempts +static short baudCnt; // counter for sync attempts at different baud rates static short ackWait; // counter of expected ACKs static uint16_t optibootVers; +static uint32_t baudRate; // baud rate at which we're programming #define RESP_SZ 64 static char responseBuf[RESP_SZ]; // buffer to accumulate responses from optiboot @@ -65,11 +70,14 @@ static void optibootTimerCB(void *); static void optibootUartRecv(char *buffer, short length); static bool processRecord(char *buf, short len); static bool programPage(void); -static void ICACHE_FLASH_ATTR armTimer(); +static void armTimer(void); +static void initBaud(void); static void ICACHE_FLASH_ATTR optibootInit() { progState = stateSync; syncCnt = 0; + baudCnt = 0; + uart0_baud(flashConfig.baud_rate); ackWait = 0; errMessage[0] = 0; responseLen = 0; @@ -88,14 +96,12 @@ static void ICACHE_FLASH_ATTR optibootInit() { // append one string to another but visually escape non-printing characters in the second // string using \x00 hex notation, max is the max chars in the concatenated string. -void ICACHE_FLASH_ATTR appendPretty(char *buf, char *raw, int max) { +void ICACHE_FLASH_ATTR appendPretty(char *buf, int max, char *raw, int rawLen) { int off = strlen(buf); - int i = 0; - while (off < max-5) { + max -= off + 1; // for null termination + for (int i=0; i= ' ' && c <= '~') { + if (c >= ' ' && c <= '~') { buf[off++] = c; } else if (c == '\n') { buf[off++] = '\\'; @@ -124,7 +130,9 @@ int ICACHE_FLASH_ATTR cgiOptibootSync(HttpdConnData *connData) { } else if (connData->requestType == HTTPD_METHOD_POST) { // issue reset optibootInit(); + baudRate = flashConfig.baud_rate; programmingCB = optibootUartRecv; + initBaud(); serbridgeReset(); #if DBG_GPIO5 makeGpio(5); @@ -147,13 +155,14 @@ int ICACHE_FLASH_ATTR cgiOptibootSync(HttpdConnData *connData) { if (!errMessage[0] && progState >= stateProg) { char buf[64]; DBG("OB got sync\n"); - os_sprintf(buf, "SYNC : Optiboot %d.%d", optibootVers>>8, optibootVers&0xff); + os_sprintf(buf, "SYNC at %ld baud: Optiboot %d.%d", + baudRate, optibootVers>>8, optibootVers&0xff); httpdSend(connData, buf, -1); } else if (errMessage[0] && progState == stateSync) { DBG("OB cannot sync\n"); char buf[512]; - os_sprintf(buf, "FAILED to SYNC: %s, got:\r\n", errMessage); - appendPretty(buf, responseBuf, 512); + os_sprintf(buf, "FAILED to SYNC: %s, got: %d chars\r\n", errMessage, responseLen); + appendPretty(buf, 512, responseBuf, responseLen); httpdSend(connData, buf, -1); } else { httpdSend(connData, errMessage[0] ? errMessage : "NOT READY", -1); @@ -306,9 +315,9 @@ int ICACHE_FLASH_ATTR cgiOptibootData(HttpdConnData *connData) { float dt = ((system_get_time() - optibootData->startTime)/1000)/1000.0; // in seconds uint16_t pgmDone = optibootData->pgmDone; optibootInit(); - os_sprintf(errMessage, "Success. %d bytes in %d.%ds, %dB/s %d%% efficient", - pgmDone, (int)dt, (int)(dt*10)%10, (int)(pgmDone/dt), - (int)(100.0*(10.0*pgmDone/flashConfig.baud_rate)/dt)); + os_sprintf(errMessage, "Success. %d bytes at %ld baud in %d.%ds, %dB/s %d%% efficient", + pgmDone, baudRate, (int)dt, (int)(dt*10)%10, (int)(pgmDone/dt), + (int)(100.0*(10.0*pgmDone/baudRate)/dt)); } else { code = 400; optibootInit(); @@ -484,24 +493,43 @@ static bool ICACHE_FLASH_ATTR programPage(void) { static void ICACHE_FLASH_ATTR armTimer() { os_timer_disarm(&optibootTimer); // time-out every 50ms, except when programming to allow for 9600baud (133ms for 128 bytes) - os_timer_arm(&optibootTimer, progState==stateProg ? 200 : 50, 0); + os_timer_arm(&optibootTimer, progState==stateProg ? PGM_INTERVAL : SYNC_INTERVAL, 0); +} + +static int baudRates[] = { 0, 9600, 57600, 115200 }; + +static void ICACHE_FLASH_ATTR setBaud() { + baudRate = baudRates[(syncCnt / (BAUD_INTERVAL/SYNC_INTERVAL)) % 4]; + uart0_baud(baudRate); + //DBG("OB changing to %d baud\n", b); +} + +static void ICACHE_FLASH_ATTR initBaud() { + baudRates[0] = flashConfig.baud_rate; + setBaud(); } static void ICACHE_FLASH_ATTR optibootTimerCB(void *arg) { // see whether we've issued so many sync in a row that it's time to give up syncCnt++; - if ((progState == stateSync && syncCnt > SYNC_TIMEOUT/50) || - syncCnt > PGM_TIMEOUT/50) { - DBG("OB abandoned after timeout, state=%d syncCnt=%d\n", progState, syncCnt); - optibootInit(); - strcpy(errMessage, "abandoned after timeout"); - return; - } - switch (progState) { case stateSync: // we're trying to get sync, all we do here is send a sync request - uart0_write_char(STK_GET_SYNC); - uart0_write_char(CRC_EOP); + if (syncCnt >= SYNC_TIMEOUT/SYNC_INTERVAL) { + // we're doomed, give up + DBG("OB sync abandoned after timeout, state=%d syncCnt=%d\n", progState, syncCnt); + optibootInit(); + strcpy(errMessage, "sync abandoned after timeout"); + return; + } + if (syncCnt % (BAUD_INTERVAL/SYNC_INTERVAL) == 0) { + // time to switch baud rate and issue a reset + setBaud(); + serbridgeReset(); + // no point sending chars if we just switched + } else { + uart0_write_char(STK_GET_SYNC); + uart0_write_char(CRC_EOP); + } break; case stateProg: // we're programming and we timed-out of inaction uart0_write_char(STK_GET_SYNC); From 9ce46d3ccc6860f9394265c7de2368f33e09fc1b Mon Sep 17 00:00:00 2001 From: Thorsten von Eicken Date: Mon, 16 Nov 2015 18:53:51 -0800 Subject: [PATCH 39/43] update readme --- README.md | 51 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 31 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index d91c514..a8cc491 100644 --- a/README.md +++ b/README.md @@ -120,8 +120,9 @@ to join its network to configure it. The short version is: use a different SSID form, such as `ai-thinker-012ABC`) 2. you join your laptop or phone to esp-link's network as a station and you configure esp-link wifi with your network info by pointing your browser at http://192.168.4.1/ - 3. esp-link starts to connect to your network while continuing to also be an access point - ("AP+STA"), the esp-link may show up with a `esp-link.local` hostname + 3. you set a hostname for esp-link on the "home" page, or leave the default ("esp-link") + 4. esp-link starts to connect to your network while continuing to also be an access point + ("AP+STA"), the esp-link may show up with a `${hostname}.local` hostname (depends on your DHCP/DNS config) 4. esp-link succeeds in connecting and shuts down its own access point after 15 seconds, you reconnect your laptop/phone to your normal network and access esp-link via its hostname @@ -136,7 +137,7 @@ status as follows: - Very short flash once every two seconds: not connected to a network and running as AP-only - Even on/off at 1HZ: connected to the configured network but no IP address (waiting on DHCP) - Steady on with very short off every 3 seconds: connected to the configured network with an - IP address (esp-link shuts down its AP after 15 seconds) + IP address (esp-link shuts down its AP after 60 seconds) The yellow "ser" LED will blink briefly every time serial data is sent or received by the esp-link. @@ -290,6 +291,12 @@ _Important_: after the initial sync request that resets the AVR you have 10 seco upload post or esp-link will time-out. So if you're manually entering curl commands have them prepared so you can copy&paste! +Beware of the baud rate, which you can set on the uC Console page. Sometimes you may be using +115200 baud in sketches but the bootloader may use 57600 baud. When you use port 23 or 2323 you +need to set the baud rate correctly. If you use the built-in programmer (HTTP POST method) then +esp-link will try the configured baud rate and also 9600, 57600, and 115200 baud, so it should +work even if you have the wrong baud rate configured... + When to use which method? If port 23 works then go with that. If you have trouble getting sync or it craps out in the middle too often then try the built-in programmer with the HTTP POST. If your AVR doesn't use optiboot then use port 2323 since esp-link may not recognize the programming @@ -299,26 +306,29 @@ If you are having trouble with the built-in programmer and see something like th ``` # ./avrflash 192.168.3.104 blink.hex Error checking sync: FAILED to SYNC: abandoned after timeout, got: -:\xF/\x00\xCj\xCz\xCJ\xCZ\xC\xAÜ\xC\xAä\xC\xAÜ\xC\xAä\xC\xBì\xC\xBô\xC\xBì\xC\xBô\xC\xAÜ\xC\xAä\xC\x00\xC\x00\x00\x00 -\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00 -\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xF6\xF6\xF6\xF6\xC +:\xF/\x00\xCj\xCz\xCJ\xCZ\xC\xAÜ\xC\xAä\xC\xAÜ\xC\xAä\xC\xBì\xC\xBô\xC\xBì\xC\xBô\xC\xAÜ\xC\xAä\xC ``` -the most likely cause is a baud rate mismatch and/or a bad connection from the esp8266 to the AVRs reset line. -The baud rate used by esp-link is set on the uC Console web page. -The above garbage characters are most likely due to optiboot timing out and starting the sketch and then the sketch -sending data at a different baud rate than configured into esp-link. -Note that sketches don't necessarily use the same baud rate as optiboot, so you may have the correct baud rate configured -but reset isn't functioning, or reset may be functioning but the baud rate may be incorrect. +the most likely cause is a baud rate mismatch and/or a bad connection from the esp8266 to the +AVRs reset line. +The baud rate used by esp-link is set on the uC Console web page and, as mentioned above, it will +automatically try 9600, 57600, and 115200 as well. +The above garbage characters are most likely due to optiboot timing out and starting the sketch +and then the sketch sending data at a different baud rate than configured into esp-link. +Note that sketches don't necessarily use the same baud rate as optiboot, so you may have the +correct baud rate configured but reset isn't functioning, or reset may be functioning but the +baud rate may be incorrect. The output of a successful flash using the built-in programmer looks like this: ``` -Success. 3098 bytes in 0.8s, 3674B/s 63% efficient +Success. 3098 bytes at 57600 baud in 0.8s, 3674B/s 63% efficient ``` -This says that the sketch comprises 3098 bytes of flash, sas written in 0.8 seconds (excludes the initial sync time), +This says that the sketch comprises 3098 bytes of flash, was written in 0.8 seconds +(excludes the initial sync time) at 57600 baud, and the 3098 bytes were flashed at a rate of 3674 bytes per second. -The efficiency measure is the ratio of the actual rate to the serial baud rate, in this case 57600 baud, +The efficiency measure is the ratio of the actual rate to the serial baud rate, thus 3674/5760 = 0.63 (there are 10 baud per character). -The efficiency is not 100% because there is protocol overhead (such as sync, record type, and length characaters) +The efficiency is not 100% because there is protocol overhead (such as sync, record type, and +length characters) and there is dead time waiting for an ack or preparing the next record to be sent. ### Flashing an attached ARM processor @@ -345,12 +355,13 @@ The esp-link web UI can display the esp-link debug log (os_printf statements in is handy but sometimes not sufficient. Esp-link also prints the debug info to the UART where it is sometimes more convenient and sometimes less... For this reason three UART debug log modes are supported that can be set in the web UI (and the mode is saved in flash): -- auto: the UART log starts enabled at boot and disables itself when esp-link associates with - an AP. It re-enables itself if the association is lost. +- auto: the UART log starts enabled at boot using uart0 and disables itself when esp-link + associates with an AP. It re-enables itself if the association is lost. - off: the UART log is always off -- on: the UART log is always on +- on0: the UART log is always on using uart0 +- on1: the UART log is always on using uart1 (gpio2 pin) -Note that even if the UART log is always off the bootloader prints to uart0 whenever the +Note that even if the UART log is always off the ROM prints to uart0 whenever the esp8266 comes out of reset. This cannot be disabled. Outbound HTTP REST requests and MQTT client From fe2c74ea3d87e353398f6add51bb99f0e8d60800 Mon Sep 17 00:00:00 2001 From: Thorsten von Eicken Date: Mon, 16 Nov 2015 19:01:09 -0800 Subject: [PATCH 40/43] update readme --- README.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/README.md b/README.md index a8cc491..8ffa55d 100644 --- a/README.md +++ b/README.md @@ -176,6 +176,25 @@ used to configure the ssid/password info. The problem is that the initial STA+AP channel 1 and you configure it to connect to an AP on channel 6. This requires the ESP8266's AP to also switch to channel 6 disconnecting you in the meantime. +Hostname, description, DHCP, mDNS +--------------------------------- +You can set a hostname on the "home" page, this should be just the hostname and not a domain +name, i.e., something like "test-module-1" and not "test-module-1.mydomain.com". +This has a number of effects: +- you will see the first 12 chars of the hostname in the menu bar (top left of the page) so + if you have multiple modules you can distinguish them visually +- esp-link will use the hostname in its DHCP request, which allows you to identify the module's + MAC and IP addresses in your DHCP server (typ. your wifi router). In addition, some DHCP + servers will inject these names into the local DNS cache so you can use URLs like + `hostname.local`. +- someday, esp-link will inject the hostname into mDNS (multicast DNS, bonjour, etc...) so + URLs of the form `hostname.local` work for everyone (as of v2.1.beta5 mDNS is disabled due + to reliability issues with it) + +You can also enter a description of up to 128 characters on the home page (bottom right). This +allows you to leave a memo for yourself, such as "installed in basement to control the heating +system". This descritpion is not used anywhere else. + Troubleshooting --------------- - verify that you have sufficient power, borderline power can cause the esp module to seemingly From df8afa82d0bc64194cc60f542fbe60fc53121b1c Mon Sep 17 00:00:00 2001 From: Thorsten von Eicken Date: Tue, 17 Nov 2015 23:32:50 -0800 Subject: [PATCH 41/43] make programming reset pulse longer for esp8266 and add notes to readme --- README.md | 17 +++++++++++++++-- esp-link/cgiwifi.c | 5 ++--- serial/serbridge.c | 2 +- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 8ffa55d..5f72622 100644 --- a/README.md +++ b/README.md @@ -365,8 +365,21 @@ it starts the connection with the `?\r\n` synchronization sequence. (This is not well tested, more details forthcoming...) Yes, you can use esp-link running on one esp8266 module to flash another esp8266 module! -For this to work you need a special version of esptool.py that has support for serial over -telnet. +The basic idea is to use some method to direct the esp8266 flash program to port 2323 of +esp-link. Using port 2323 with the appropriate wiring will cause the esp8266's reset and +gpio0 pins to be toggled such that the chip enters the flash programming mode. + +One option for connecting the programmer with esp-link is to use my version of esptool.py +at http://github.com/tve/esptool, which supports specifying a URL instead of a port. Thus +instead of specifying something like `--port /dev/ttyUSB0` or `--port COM1` you specify +`--port socket://esp-link.local:2323`. Important: the baud rate specified on the esptool.py +command-line is irrelevant as the baud rate used by esp-link will be the one set in the +uC console page. Fortunately the esp8266 bootloader does auto-baud detection. (Setting the +baud rate to 115200 is recommended.) + +Another option is to use a serial-to-tcp port forwarding driver and point that to port 2323 +of esp-link. On windows users have reported success with +[HW Virtual Serial Port](http://www.hw-group.com/products/hw_vsp/hw_vsp2_en.html) Debug log --------- diff --git a/esp-link/cgiwifi.c b/esp-link/cgiwifi.c index a56eee2..dc190db 100644 --- a/esp-link/cgiwifi.c +++ b/esp-link/cgiwifi.c @@ -113,7 +113,7 @@ wifiAddStateChangeCb(WifiStateChangeCb cb) { DBG("WIFI: max state change cb count exceeded\n"); } -static bool mdns_started = true; +static bool mdns_started = false; static struct mdns_info mdns_info; // cannot allocate the info struct on the stack, it crashes! @@ -126,7 +126,6 @@ void wifiStartMDNS(struct ip_addr ip) { mdns_info.server_port = 80, // service port mdns_info.ipAddr = ip.addr, espconn_mdns_init(&mdns_info); - espconn_mdns_enable(); mdns_started = true; } } @@ -237,7 +236,7 @@ static int ICACHE_FLASH_ATTR cgiWiFiGetScan(HttpdConnData *connData) { while (pos < cgiWifiAps.noAps && pos < next+chunk) { len += os_sprintf(buff+len, "{\"essid\": \"%s\", \"rssi\": %d, \"enc\": \"%d\"}%c\n", cgiWifiAps.apData[pos]->ssid, cgiWifiAps.apData[pos]->rssi, cgiWifiAps.apData[pos]->enc, - (pos+1 == cgiWifiAps.noAps) ? ' ' : ','); + (pos+1 == cgiWifiAps.noAps) ? ' ' : ','); pos++; } // done or more? diff --git a/serial/serbridge.c b/serial/serbridge.c index c996b9a..7814d88 100644 --- a/serial/serbridge.c +++ b/serial/serbridge.c @@ -211,7 +211,7 @@ serbridgeRecvCb(void *arg, char *data, unsigned short len) if (mcu_reset_pin >= 0) GPIO_OUTPUT_SET(mcu_reset_pin, 0); os_delay_us(100L); if (mcu_isp_pin >= 0) GPIO_OUTPUT_SET(mcu_isp_pin, 0); - os_delay_us(100L); + os_delay_us(2000L); if (mcu_reset_pin >= 0) GPIO_OUTPUT_SET(mcu_reset_pin, 1); //os_delay_us(100L); //if (mcu_isp_pin >= 0) GPIO_OUTPUT_SET(mcu_isp_pin, 1); From 72118a7627a1de6b2bd37975d5eb9b32a6f0484c Mon Sep 17 00:00:00 2001 From: Thorsten von Eicken Date: Wed, 18 Nov 2015 22:17:34 -0800 Subject: [PATCH 42/43] fix dhcp/static ip switch-over --- esp-link/cgiwifi.c | 10 +++------- html/wifi/wifi.js | 1 - 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/esp-link/cgiwifi.c b/esp-link/cgiwifi.c index dc190db..0fcbf71 100644 --- a/esp-link/cgiwifi.c +++ b/esp-link/cgiwifi.c @@ -428,7 +428,6 @@ void ICACHE_FLASH_ATTR configWifiIP() { // Change special settings int ICACHE_FLASH_ATTR cgiWiFiSpecial(HttpdConnData *connData) { char dhcp[8]; - char hostname[32]; char staticip[20]; char netmask[20]; char gateway[20]; @@ -437,12 +436,11 @@ int ICACHE_FLASH_ATTR cgiWiFiSpecial(HttpdConnData *connData) { // get args and their string lengths int dl = httpdFindArg(connData->getArgs, "dhcp", dhcp, sizeof(dhcp)); - int hl = httpdFindArg(connData->getArgs, "hostname", hostname, sizeof(hostname)); int sl = httpdFindArg(connData->getArgs, "staticip", staticip, sizeof(staticip)); int nl = httpdFindArg(connData->getArgs, "netmask", netmask, sizeof(netmask)); int gl = httpdFindArg(connData->getArgs, "gateway", gateway, sizeof(gateway)); - if (!(dl > 0 && hl >= 0 && sl >= 0 && nl >= 0 && gl >= 0)) { + if (!(dl > 0 && sl >= 0 && nl >= 0 && gl >= 0)) { jsonHeader(connData, 400); httpdSend(connData, "Request is missing fields", -1); return HTTPD_CGI_DONE; @@ -470,11 +468,9 @@ int ICACHE_FLASH_ATTR cgiWiFiSpecial(HttpdConnData *connData) { os_sprintf(url, "{\"url\": \"http://%d.%d.%d.%d\"}", IP2STR(&ipi.ip)); } else { - // no static IP, set hostname - if (hl == 0) os_strcpy(hostname, "esp-link"); + // dynamic IP flashConfig.staticip = 0; - os_strcpy(flashConfig.hostname, hostname); - os_sprintf(url, "{\"url\": \"http://%s\"}", hostname); + os_sprintf(url, "{\"url\": \"http://%s\"}", flashConfig.hostname); } configSave(); // ignore error... diff --git a/html/wifi/wifi.js b/html/wifi/wifi.js index 82254dc..1718784 100644 --- a/html/wifi/wifi.js +++ b/html/wifi/wifi.js @@ -175,7 +175,6 @@ function changeSpecial(e) { e.preventDefault(); var url = "special"; url += "?dhcp=" + document.querySelector('input[name="dhcp"]:checked').value; - url += "&hostname=" + encodeURIComponent($("#wifi-hostname").value); url += "&staticip=" + encodeURIComponent($("#wifi-staticip").value); url += "&netmask=" + encodeURIComponent($("#wifi-netmask").value); url += "&gateway=" + encodeURIComponent($("#wifi-gateway").value); From cb7539655278eabd111c25db8fdbd263a07ec299 Mon Sep 17 00:00:00 2001 From: Thorsten von Eicken Date: Wed, 18 Nov 2015 22:17:49 -0800 Subject: [PATCH 43/43] extend reset for esp8266 --- serial/serbridge.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/serial/serbridge.c b/serial/serbridge.c index 7814d88..8d400fd 100644 --- a/serial/serbridge.c +++ b/serial/serbridge.c @@ -145,7 +145,7 @@ serbridgeReset() os_printf("MCU reset gpio%d\n", mcu_reset_pin); #endif GPIO_OUTPUT_SET(mcu_reset_pin, 0); - os_delay_us(100L); + os_delay_us(2000L); // esp8266 needs at least 1ms reset pulse, it seems... GPIO_OUTPUT_SET(mcu_reset_pin, 1); } #ifdef SERBR_DBG @@ -320,7 +320,7 @@ static void ICACHE_FLASH_ATTR serbridgeSentCb(void *arg) { serbridgeConnData *conn = ((struct espconn*)arg)->reverse; - os_printf("Sent CB %p\n", conn); + //os_printf("Sent CB %p\n", conn); if (conn == NULL) return; //os_printf("%d ST\n", system_get_time()); if (conn->sentbuffer != NULL) os_free(conn->sentbuffer);