diff --git a/esp-utils/common/serialport.cpp b/esp-utils/common/serialport.cpp index e6d957f..d4b1e8c 100644 --- a/esp-utils/common/serialport.cpp +++ b/esp-utils/common/serialport.cpp @@ -18,6 +18,7 @@ SerialPort::SerialPort(COM comport) { mReadError = false; mBytesRecivedSinceLastSend = 0; mThread = 0; + mLastReceived = 0; } COM SerialPort::get_com() { @@ -37,6 +38,11 @@ bool SerialPort::waitAnswer(unsigned int len, unsigned int timeOutMs) { return false; } +void SerialPort::waitTransmitionEnd(unsigned int timeOutMs) { + while(getTick() - mLastReceived < timeOutMs) + sleep(10); +} + void SerialPort::send(const char *text) { mBytesRecivedSinceLastSend = 0; unsigned int tlen = strlen(text); diff --git a/esp-utils/common/serialport.h b/esp-utils/common/serialport.h index 3218585..9607013 100644 --- a/esp-utils/common/serialport.h +++ b/esp-utils/common/serialport.h @@ -36,6 +36,7 @@ class SerialPort void send(char c); void send(const void *data, unsigned int len); bool waitAnswer(unsigned int len, unsigned int timeOutMs); + void waitTransmitionEnd(unsigned int timeOutMs); bool isReadError(); static const char *findNextPort(bool finish); private: @@ -48,12 +49,14 @@ class SerialPort unsigned int write_native(const void *data, unsigned int len); bool read_native(const void *data, unsigned int len, unsigned int *rb); void sleep(unsigned int ms); + static unsigned int getTick(); COM get_com(); COM mCom; bool mTreadFlag; void *mThread; unsigned int mBytesRecivedSinceLastSend; bool mReadError; + unsigned int mLastReceived; }; extern void SerialPortRecieved(SerialPort *port, const char *text, unsigned int len); diff --git a/esp-utils/common/serialport_posix.cpp b/esp-utils/common/serialport_posix.cpp index be9f398..1182309 100644 --- a/esp-utils/common/serialport_posix.cpp +++ b/esp-utils/common/serialport_posix.cpp @@ -20,6 +20,7 @@ #include #include #include +#include void * SerialPort::thread_start(void *arg) { SerialPort *port = (SerialPort *)arg; @@ -32,6 +33,7 @@ void * SerialPort::thread_start(void *arg) { if(!port->mTreadFlag) break; if(rb>0) { + port->mLastReceived = port->getTick(); port->mReadError = false; buff[rb]='\0'; SerialPortRecieved(port, buff, rb); @@ -142,4 +144,10 @@ const char *SerialPort::findNextPort(bool finish) { return buf; } +unsigned int SerialPort::getTick() { + struct timeval tv; + gettimeofday(&tv,NULL); + return tv.tv_usec / 1000UL + (tv.tv_sec % 3600000UL) * 1000UL; +} + #endif diff --git a/esp-utils/common/serialport_win.cpp b/esp-utils/common/serialport_win.cpp index 3ec03ce..cbe32f4 100644 --- a/esp-utils/common/serialport_win.cpp +++ b/esp-utils/common/serialport_win.cpp @@ -27,6 +27,7 @@ DWORD SerialPort::ThreadProc (LPVOID lpdwThreadParam ) { break; if(res) { if(read > 0) { + port->mLastReceived = port->getTick(); port->mReadError = false; buff[read]='\0'; SerialPortRecieved(port, buff, read); @@ -125,4 +126,8 @@ const char *SerialPort::findNextPort(bool finish) { return buf; } +unsigned int SerialPort::getTick() { + return GetTickCount(); +} + #endif diff --git a/esp-utils/esp-flasher.cpp b/esp-utils/esp-flasher.cpp index a544a70..20fd5a1 100644 --- a/esp-utils/esp-flasher.cpp +++ b/esp-utils/esp-flasher.cpp @@ -35,6 +35,15 @@ typedef struct { uint32_t cs; } ESP_REQUEST_HEADER; +uint8_t ESP_INIT_DATA_DEAFULT[] = { 0x05, 0x00, 0x04, 0x02, 0x05, 0x05, 0x05, 0x02, 0x05, 0x00, 0x04, 0x05, 0x05, 0x04, 0x05, 0x05, + 0x04, 0xfe, 0xfd, 0xff, 0xf0, 0xf0, 0xf0, 0xe0, 0xe0, 0xe0, 0xe1, 0x0a, 0xff, 0xff, 0xf8, 0x00, + 0xf8, 0xf8, 0x52, 0x4e, 0x4a, 0x44, 0x40, 0x38, 0x00, 0x00, 0x01, 0x01, 0x02, 0x03, 0x04, 0x05, + 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0xe1, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x93, 0x43, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; + void SerialPortError(SerialPort */*port*/,const char *text) { printf("\r\n%s\r\n", text); @@ -129,7 +138,7 @@ bool flash_send(SerialPort *port, ESP_REQUEST_HEADER *hdr, void *body, bool prin // printf("\r\n"); port->send(oBuf, oBufPos); - if (!port->waitAnswer(9, TIMEOUT)) { + if (!port->waitAnswer(5, TIMEOUT)) { if(printErrors) printf("\r\nNo answer from device\r\n"); return false; @@ -141,11 +150,12 @@ bool flash_send(SerialPort *port, ESP_REQUEST_HEADER *hdr, void *body, bool prin return false; } uint32_t asize = (uint32_t) recivedBuf[3] | (uint32_t) (recivedBuf[4] << 8); - if (!port->waitAnswer(asize + 3, TIMEOUT)) { + if (!port->waitAnswer(asize + 10, TIMEOUT)) { if(printErrors) printf("\r\nAnswer not completed\r\n"); return false; } + port->waitTransmitionEnd(100); if (asize != 2 || recivedBuf[asize + 9] != 0xc0) { if(printErrors) printf("\r\nWrong body length\r\n"); @@ -160,16 +170,7 @@ bool flash_send(SerialPort *port, ESP_REQUEST_HEADER *hdr, void *body, bool prin return true; } -bool flash_file(SerialPort *port, char *file, uint32_t address) { - FILE* fd = fopen(file, "rb"); - if (!fd) { - printf("\r\nFailed to open file %s\r\n", file); - return false; - } - fseek(fd, 0, SEEK_END); - uint32_t size = ftell(fd); - fseek(fd, 0, SEEK_SET); - printf("Flashing %s at 0x%08X\r\n", file, address); +bool flash_mem(SerialPort *port, uint8_t * buf, uint32_t size, uint32_t address) { uint32_t blocks_count = ceil((double) size / (double) ESP_BLOCK_SIZE); ESP_REQUEST_HEADER rh; uint32_t pbody[4]; @@ -187,12 +188,10 @@ bool flash_file(SerialPort *port, char *file, uint32_t address) { printf("\r\nFailed to enter flash mode\r\n"); return false; } - printf("Total block count %d, block size %d\r\n", blocks_count, - ESP_BLOCK_SIZE); + printf("Total block count %d, block size %d\r\n", blocks_count, ESP_BLOCK_SIZE); for (unsigned int seq = 0; seq < blocks_count; seq++) { - printf("\rWriting block %d/%d at 0x%08X ", seq, blocks_count, - address + ESP_BLOCK_SIZE * seq); + printf("\rWriting block %d/%d at 0x%08X ", seq, blocks_count, address + ESP_BLOCK_SIZE * seq); uint8_t buffer[ESP_BLOCK_SIZE + 16]; uint8_t *data = (uint8_t *) &buffer[16]; uint32_t *data32 = (uint32_t *) buffer; @@ -200,31 +199,51 @@ bool flash_file(SerialPort *port, char *file, uint32_t address) { data32[1] = htole32(seq); data32[2] = 0; data32[3] = 0; - unsigned int rb = fread(data, 1, ESP_BLOCK_SIZE, fd); - if(rb == 0) { - fclose(fd); - printf("\r\nFailed to read image file\r\n"); - return false; - } else if (rb < ESP_BLOCK_SIZE) { - memset(&data[rb], 0xff, ESP_BLOCK_SIZE - rb); + uint32_t bl = size - seq * ESP_BLOCK_SIZE; + if (bl < ESP_BLOCK_SIZE) { + memcpy(data, &buf[seq * ESP_BLOCK_SIZE], bl); + memset(&data[bl], 0xff, ESP_BLOCK_SIZE - bl); + } else { + memcpy(data, &buf[seq * ESP_BLOCK_SIZE], ESP_BLOCK_SIZE); } rh.magic = 0x00; rh.command = 0x03; // flash data rh.size = htole16(sizeof(buffer)); rh.cs = esp_checksum(data, ESP_BLOCK_SIZE); if (!flash_send(port, &rh, buffer, true)) { - fclose(fd); printf("\r\nFailed to flash\r\n"); return false; } } - printf("\rBlocks wrote %d/%d at 0x%08X\r\n", blocks_count, blocks_count, - address); - - fclose(fd); + printf("\rBlocks wrote %d/%d at 0x%08X\r\n", blocks_count, blocks_count, address); return true; } +bool flash_file(SerialPort *port, char *file, uint32_t address) { + FILE* fd = fopen(file, "rb"); + if (!fd) { + printf("\r\nFailed to open file %s\r\n", file); + return false; + } + fseek(fd, 0, SEEK_END); + uint32_t size = ftell(fd); + fseek(fd, 0, SEEK_SET); + printf("Flashing %s at 0x%08X\r\n", file, address); + uint8_t *data = new uint8_t[size]; + + unsigned int rb = fread(data, 1, size, fd); + fclose (fd); + if (rb != size) { + delete [] data; + printf("\r\nFailed to read image file\r\n"); + return false; + } + bool res = flash_mem(port, data, size, address); + + delete [] data; + return res; +} + bool flash_done(SerialPort *port) { uint32_t reboot = htole32(1); ESP_REQUEST_HEADER rh; @@ -336,10 +355,24 @@ int main(int argc, char* argv[]) { "esp-flasher 0x00000 image1.bin 0x40000 image2.bin\r\n" "Trying to flash with defaults:\r\n" \ "0x00000 <- 0x00000.bin\r\n" \ - "0x40000 <- 0x40000.bin\r\n"); + "0x40000 <- 0x40000.bin\r\n" \ + "0x7C000 <- default configuration\r\n" \ + "0x7E000 <- 4K of 0xFF\r\n" + ); isSuccess = flash_file(port, (char*)"0x00000.bin", 0x00000); - if(isSuccess) + if(isSuccess) { isSuccess = flash_file(port, (char*)"0x40000.bin", 0x40000); + if(isSuccess) { + printf("Flashing default configuration at 0x%08X\r\n", 0x7C000); + isSuccess = flash_mem(port, ESP_INIT_DATA_DEAFULT, sizeof(ESP_INIT_DATA_DEAFULT), 0x7C000); + if(isSuccess) { + uint8_t data[4*1024]; + memset(data, 0xFF, sizeof(data)); + printf("Flashing 4K of 0xFF at 0x%08X\r\n", 0x7E000); + isSuccess = flash_mem(port, data, sizeof(data), 0x7E000); + } + } + } } else { if((argc - currentArg) % 2) { delete port; diff --git a/release/dh-esp-firmware-v0.2.7z b/release/dh-esp-firmware-v0.2.7z index d23b1c3..495dea7 100644 Binary files a/release/dh-esp-firmware-v0.2.7z and b/release/dh-esp-firmware-v0.2.7z differ diff --git a/release/utils/esp-flasher-linux b/release/utils/esp-flasher-linux index 681ab6b..276e8ee 100755 Binary files a/release/utils/esp-flasher-linux and b/release/utils/esp-flasher-linux differ diff --git a/release/utils/esp-flasher-osx b/release/utils/esp-flasher-osx index dd5f8fd..496913d 100755 Binary files a/release/utils/esp-flasher-osx and b/release/utils/esp-flasher-osx differ diff --git a/release/utils/esp-flasher-win.exe b/release/utils/esp-flasher-win.exe index e79e16c..a2ae760 100644 Binary files a/release/utils/esp-flasher-win.exe and b/release/utils/esp-flasher-win.exe differ diff --git a/release/utils/esp-terminal-linux b/release/utils/esp-terminal-linux index 4c49869..c4a99ac 100755 Binary files a/release/utils/esp-terminal-linux and b/release/utils/esp-terminal-linux differ diff --git a/release/utils/esp-terminal-osx b/release/utils/esp-terminal-osx index 937c1b6..6b94a28 100755 Binary files a/release/utils/esp-terminal-osx and b/release/utils/esp-terminal-osx differ diff --git a/release/utils/esp-terminal-win.exe b/release/utils/esp-terminal-win.exe index 7d18f69..e0c659c 100644 Binary files a/release/utils/esp-terminal-win.exe and b/release/utils/esp-terminal-win.exe differ