Skip to content

Commit

Permalink
Merge pull request esp8266#512 from esp8266/feature/ota
Browse files Browse the repository at this point in the history
OTA
  • Loading branch information
igrr committed Jul 6, 2015
2 parents a82796f + 5763dbb commit 7891a84
Show file tree
Hide file tree
Showing 15 changed files with 683 additions and 111 deletions.
22 changes: 17 additions & 5 deletions boards.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
menu.UploadSpeed=Upload Speed
menu.CpuFrequency=CPU Frequency
menu.FlashSize=Flash Size
menu.FlashMode=Flash Mode
menu.FlashFreq=Flash Frequency
menu.UploadTool=Upload Using

##############################################################
generic.name=Generic ESP8266 Module
Expand All @@ -23,11 +25,26 @@ generic.build.variant=generic
generic.build.flash_mode=qio
generic.build.spiffs_pagesize=256

generic.menu.UploadTool.esptool=Serial
generic.menu.UploadTool.esptool.upload.tool=esptool
generic.menu.UploadTool.espota=OTA
generic.menu.UploadTool.espota.upload.tool=espota

generic.menu.CpuFrequency.80=80 MHz
generic.menu.CpuFrequency.80.build.f_cpu=80000000L
generic.menu.CpuFrequency.160=160 MHz
generic.menu.CpuFrequency.160.build.f_cpu=160000000L

generic.menu.FlashFreq.40=40MHz
generic.menu.FlashFreq.40.build.flash_freq=40
generic.menu.FlashFreq.80=80MHz
generic.menu.FlashFreq.80.build.flash_freq=80

generic.menu.FlashMode.dio=DIO
generic.menu.FlashMode.dio.build.flash_mode=dio
generic.menu.FlashMode.qio=QIO
generic.menu.FlashMode.qio.build.flash_mode=qio

generic.menu.UploadSpeed.115200=115200
generic.menu.UploadSpeed.115200.upload.speed=115200
generic.menu.UploadSpeed.9600=9600
Expand Down Expand Up @@ -117,11 +134,6 @@ generic.menu.FlashSize.4M.upload.maximum_size=1044464
# generic.menu.FlashSize.16M.build.spiffs_end=0x1000000
# generic.menu.FlashSize.16M.build.spiffs_blocksize=8192

generic.menu.FlashFreq.40=40MHz
generic.menu.FlashFreq.40.build.flash_freq=40
generic.menu.FlashFreq.80=80MHz
generic.menu.FlashFreq.80.build.flash_freq=80

##############################################################
modwifi.name=Olimex MOD-WIFI-ESP8266(-DEV)

Expand Down
5 changes: 3 additions & 2 deletions bootloaders/eboot/Makefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
XTENSA_TOOLCHAIN ?=
XTENSA_TOOLCHAIN ?= ../../tools/xtensa-lx106-elf/bin/
ESPTOOL ?= ../../tools/esptool

BIN_DIR := ./
TARGET_DIR := ./

TARGET_OBJ_FILES := \
eboot.o \
eboot_command.o \
flash.o \


TARGET_OBJ_PATHS := $(addprefix $(TARGET_DIR)/,$(TARGET_OBJ_FILES))

Expand Down
56 changes: 25 additions & 31 deletions bootloaders/eboot/eboot.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,38 +76,31 @@ int copy_raw(const uint32_t src_addr,
const uint32_t dst_addr,
const uint32_t size)
{
ets_putc('\n');
ets_putc('c');
ets_putc('p');
ets_putc('\n');
// require regions to be aligned
if (src_addr & 0xfff != 0 ||
dst_addr & 0xfff != 0) {
return 1;
}

if (SPIEraseAreaEx(dst_addr, size)) {
return 2;
}

const uint32_t buffer_size = 4096;
const uint32_t buffer_size = FLASH_SECTOR_SIZE;
uint8_t buffer[buffer_size];

const uint32_t end = src_addr + size;
uint32_t left = ((size+buffer_size-1) & ~(buffer_size-1));
uint32_t saddr = src_addr;
uint32_t daddr = dst_addr;
uint32_t left = size;
while (saddr < end) {
uint32_t will_copy = (left < buffer_size) ? left : buffer_size;
if (SPIRead(saddr, buffer, will_copy)) {
return 3;
}
if (SPIWrite(daddr, buffer, will_copy)) {
return 4;
}
saddr += will_copy;
daddr += will_copy;
left -= will_copy;

while (left) {
if (SPIEraseSector(daddr/buffer_size)) {
return 2;
}
if (SPIRead(saddr, buffer, buffer_size)) {
return 3;
}
if (SPIWrite(daddr, buffer, buffer_size)) {
return 4;
}
saddr += buffer_size;
daddr += buffer_size;
left -= buffer_size;
}

return 0;
Expand All @@ -123,30 +116,31 @@ void main()
if (eboot_command_read(&cmd)) {
cmd.action = ACTION_LOAD_APP;
cmd.args[0] = 0;
ets_putc('e');
ets_putc('~');
} else {
ets_putc('@');
}
eboot_command_clear();

if (cmd.action == ACTION_COPY_RAW) {
ets_putc('c'); ets_putc('p'); ets_putc(':');
res = copy_raw(cmd.args[0], cmd.args[1], cmd.args[2]);
ets_putc('0'+res); ets_putc('\n');
if (res == 0) {
cmd.action = ACTION_LOAD_APP;
cmd.args[0] = cmd.args[1];
}
}

if (cmd.action == ACTION_LOAD_APP) {
res = load_app_from_flash_raw(cmd.args[0]);
ets_putc('l'); ets_putc('d'); ets_putc('\n');
res = load_app_from_flash_raw(cmd.args[0]);
//we will get to this only on load fail
ets_putc('e'); ets_putc(':'); ets_putc('0'+res); ets_putc('\n');
}

if (res) {
ets_putc('\n');
ets_putc('#');
ets_putc('0' + res);
ets_putc('\n');
SWRST;
SWRST;
}

while(true){}
Expand Down
Binary file modified bootloaders/eboot/eboot.elf
Binary file not shown.
2 changes: 1 addition & 1 deletion bootloaders/eboot/flash.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Redistribution and use is permitted according to the conditions of the
* 3-clause BSD license to be found in the LICENSE file.
*/

#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
Expand Down
1 change: 1 addition & 0 deletions cores/esp8266/Arduino.h
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ void loop(void);

#include "HardwareSerial.h"
#include "Esp.h"
#include "Updater.h"
#include "debug.h"

#define min(a,b) ((a)<(b)?(a):(b))
Expand Down
93 changes: 24 additions & 69 deletions cores/esp8266/Esp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ extern struct rst_info resetInfo;
}


// #define DEBUG_SERIAL Serial
//#define DEBUG_SERIAL Serial


/**
Expand Down Expand Up @@ -358,83 +358,38 @@ uint32_t EspClass::getFreeSketchSpace() {
return freeSpaceEnd - freeSpaceStart;
}

bool EspClass::updateSketch(Stream& in, uint32_t size) {

if (size > getFreeSketchSpace())
return false;

uint32_t usedSize = getSketchSize();
uint32_t freeSpaceStart = (usedSize + FLASH_SECTOR_SIZE - 1) & (~(FLASH_SECTOR_SIZE - 1));
uint32_t roundedSize = (size + FLASH_SECTOR_SIZE - 1) & (~(FLASH_SECTOR_SIZE - 1));

bool EspClass::updateSketch(Stream& in, uint32_t size, bool restartOnFail, bool restartOnSuccess) {
if(!Update.begin(size)){
#ifdef DEBUG_SERIAL
DEBUG_SERIAL.printf("erase @0x%x size=0x%x\r\n", freeSpaceStart, roundedSize);
DEBUG_SERIAL.print("Update ");
Update.printError(DEBUG_SERIAL);
#endif
if(restartOnFail) ESP.restart();
return false;
}

noInterrupts();
int rc = SPIEraseAreaEx(freeSpaceStart, roundedSize);
interrupts();
if (rc)
return false;

if(Update.writeStream(in) != size){
#ifdef DEBUG_SERIAL
DEBUG_SERIAL.println("erase done");
DEBUG_SERIAL.print("Update ");
Update.printError(DEBUG_SERIAL);
#endif
if(restartOnFail) ESP.restart();
return false;
}

uint32_t addr = freeSpaceStart;
uint32_t left = size;

const uint32_t bufferSize = FLASH_SECTOR_SIZE;
std::unique_ptr<uint8_t> buffer(new uint8_t[bufferSize]);

#ifdef DEBUG_SERIAL
DEBUG_SERIAL.println("writing");
#endif
while (left > 0) {
size_t willRead = (left < bufferSize) ? left : bufferSize;
size_t rd = in.readBytes(buffer.get(), willRead);
if (rd != willRead) {
if(!Update.end()){
#ifdef DEBUG_SERIAL
DEBUG_SERIAL.println("stream read failed");
DEBUG_SERIAL.print("Update ");
Update.printError(DEBUG_SERIAL);
#endif
return false;
}

if(addr == freeSpaceStart) {
// check for valid first magic byte
if(*((uint8 *) buffer.get()) != 0xE9) {
return false;
}
}

noInterrupts();
rc = SPIWrite(addr, buffer.get(), willRead);
interrupts();
if (rc) {
#ifdef DEBUG_SERIAL
DEBUG_SERIAL.println("write failed");
#endif
return false;
}
if(restartOnFail) ESP.restart();
return false;
}

addr += willRead;
left -= willRead;
#ifdef DEBUG_SERIAL
DEBUG_SERIAL.print(".");
#endif
}

#ifdef DEBUG_SERIAL
DEBUG_SERIAL.println("\r\nrestarting");
#endif
eboot_command ebcmd;
ebcmd.action = ACTION_COPY_RAW;
ebcmd.args[0] = freeSpaceStart;
ebcmd.args[1] = 0x00000;
ebcmd.args[2] = size;
eboot_command_write(&ebcmd);

ESP.restart();
return true; // never happens
DEBUG_SERIAL.println("Update SUCCESS");
#endif
if(restartOnSuccess) ESP.restart();
return true;
}

2 changes: 1 addition & 1 deletion cores/esp8266/Esp.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class EspClass {

uint32_t getSketchSize();
uint32_t getFreeSketchSpace();
bool updateSketch(Stream& in, uint32_t size);
bool updateSketch(Stream& in, uint32_t size, bool restartOnFail = false, bool restartOnSuccess = true);

String getResetInfo();
struct rst_info * getResetInfoPtr();
Expand Down
Loading

0 comments on commit 7891a84

Please sign in to comment.