Skip to content

Commit

Permalink
Fix ESP32 TasmotaClient firmware upgrade
Browse files Browse the repository at this point in the history
Fix ESP32 TasmotaClient firmware upgrade (#9218)
  • Loading branch information
arendst committed Nov 25, 2020
1 parent 7f58155 commit 9433b69
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 73 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ All notable changes to this project will be documented in this file.
### Fixed
- KNX ESP32 UDP mulicastpackage (#9811)
- Command ``gpio`` using non-indexed functions regression from v9.1.0 (#9962)
- ESP32 TasmotaClient firmware upgrade (#9218)

### Removed

Expand Down
1 change: 1 addition & 0 deletions RELEASENOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ The attached binaries can also be downloaded from http://ota.tasmota.com/tasmota
- KNX buttons, switches and sensors detection regression from v9.1.0 (#9811)
- KNX ESP32 UDP mulicastpackage (#9811)
- GUI MqttUser and MqttPassword updates when TLS is compiled in (#9825)
- ESP32 TasmotaClient firmware upgrade (#9218)

### Removed
- Version compatibility check
3 changes: 1 addition & 2 deletions tasmota/xdrv_01_webserver.ino
Original file line number Diff line number Diff line change
Expand Up @@ -2893,8 +2893,7 @@ void HandleUploadLoop(void)

AddLog_P(LOG_LEVEL_INFO, PSTR(D_LOG_UPLOAD "Transfer %u bytes"), upload.totalSize);

// uint8_t* data = (uint8_t*)(0x40200000 + (FlashWriteStartSector() * SPI_FLASH_SEC_SIZE));
uint8_t* data = FlashDirectAccess();
uint8_t* data = FlashDirectAccess();

// uint32_t* values = (uint32_t*)(data); // Only 4-byte access allowed
// AddLog_P(LOG_LEVEL_DEBUG, PSTR(D_LOG_UPLOAD "Head 0x%08X"), values[0]);
Expand Down
118 changes: 47 additions & 71 deletions tasmota/xdrv_31_tasmota_client.ino
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ uint32_t SimpleHexParseLine(char *hexline) {

// AddLog_P(LOG_LEVEL_DEBUG, PSTR("DBG: Hexline |%s|, Len %d, Address 0x%02X%02X, RecType %d"), hexline, len, addr_h, addr_l, rectype);

if (len > 16) { return 4; } // Error 4: Line too long
if (rectype > 1) { return 5; } // Error 5: Invalid record type
if (len > 16) { return 5; } // Error: Line too long
if (rectype > 1) { return 6; } // Error: Invalid record type

for (uint8_t idx = 0; idx < len; idx++) {
if (SHParse.FlashPageIdx < sizeof(SHParse.FlashPage)) {
Expand Down Expand Up @@ -238,8 +238,8 @@ uint8_t TasmotaClient_receiveData(char* buffer, int size) {
}
if (255 == index) { index = 0; }

AddLog_P(LOG_LEVEL_DEBUG, PSTR("TCL: ReceiveData"));
AddLogBuffer(LOG_LEVEL_DEBUG, (uint8_t*)buffer, index);
// AddLog_P(LOG_LEVEL_DEBUG, PSTR("TCL: ReceiveData"));
// AddLogBuffer(LOG_LEVEL_DEBUG, (uint8_t*)buffer, index);

return index;
}
Expand Down Expand Up @@ -279,11 +279,27 @@ uint8_t TasmotaClient_exitProgMode(void) {
return TasmotaClient_execCmd(CMND_STK_LEAVE_PROGMODE); // Exit programming mode
}

uint8_t TasmotaClient_SetupFlash(void) {
uint8_t ProgParams[] = {0x86, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x03, 0xff, 0xff, 0xff, 0xff, 0x00, 0x80, 0x04, 0x00, 0x00, 0x00, 0x80, 0x00};
uint8_t ExtProgParams[] = {0x05, 0x04, 0xd7, 0xc2, 0x00};
uint8_t TasmotaClient_loadAddress(uint8_t adrHi, uint8_t adrLo) {
uint8_t params[] = { adrLo, adrHi };
return TasmotaClient_execParam(CMND_STK_LOAD_ADDRESS, params, sizeof(params));
}

void TasmotaClient_flashPage(uint8_t addr_h, uint8_t addr_l, uint8_t* data) {
uint8_t Header[] = {CMND_STK_PROG_PAGE, 0x00, 0x80, 0x46};
TasmotaClient_loadAddress(addr_h, addr_l);
TasmotaClient_Serial->write(Header, 4);
for (int i = 0; i < 128; i++) {
TasmotaClient_Serial->write(data[i]);
}
TasmotaClient_Serial->write(CONST_STK_CRC_EOP);
TasmotaClient_waitForSerialData(2, TASMOTA_CLIENT_TIMEOUT);
TasmotaClient_Serial->read();
TasmotaClient_Serial->read();
}

uint32_t TasmotaClient_Flash(uint8_t* data, size_t size) {
/*
// Don't do this as there is no re-init configured
TasmotaClient_Serial->end();
delay(10);
Expand All @@ -292,71 +308,32 @@ uint8_t TasmotaClient_SetupFlash(void) {
ClaimSerial();
}
*/

TasmotaClient_Reset();

uint8_t timeout = 0;
uint8_t no_error = 0;
while (50 > timeout) {
while (timeout <= 50) {
if (TasmotaClient_execCmd(CMND_STK_GET_SYNC)) {
timeout = 200;
no_error = 1;
break;
}
timeout++;
delay(1);
}
if (no_error) {
AddLog_P(LOG_LEVEL_INFO, PSTR("TCL: Found bootloader"));
} else {
no_error = 0;
AddLog_P(LOG_LEVEL_INFO, PSTR("TCL: Bootloader could not be found"));
}
if (no_error) {
if (TasmotaClient_execParam(CMND_STK_SET_DEVICE, ProgParams, sizeof(ProgParams))) {
} else {
no_error = 0;
AddLog_P(LOG_LEVEL_INFO, PSTR("TCL: Could not configure device for programming (1)"));
}
}
if (no_error) {
if (TasmotaClient_execParam(CMND_STK_SET_DEVICE_EXT, ExtProgParams, sizeof(ExtProgParams))) {
} else {
no_error = 0;
AddLog_P(LOG_LEVEL_INFO, PSTR("TCL: Could not configure device for programming (2)"));
}
}
if (no_error) {
if (TasmotaClient_execCmd(CMND_STK_ENTER_PROGMODE)) {
} else {
no_error = 0;
AddLog_P(LOG_LEVEL_INFO, PSTR("TCL: Failed to put bootloader into programming mode"));
}
}
return no_error;
}
if (timeout > 50) { return 1; } // Error: Bootloader could not be found

uint8_t TasmotaClient_loadAddress(uint8_t adrHi, uint8_t adrLo) {
uint8_t params[] = { adrLo, adrHi };
return TasmotaClient_execParam(CMND_STK_LOAD_ADDRESS, params, sizeof(params));
}
AddLog_P(LOG_LEVEL_INFO, PSTR("TCL: Found bootloader"));

void TasmotaClient_flashPage(uint8_t addr_h, uint8_t addr_l, uint8_t* data) {
uint8_t Header[] = {CMND_STK_PROG_PAGE, 0x00, 0x80, 0x46};
TasmotaClient_loadAddress(addr_h, addr_l);
TasmotaClient_Serial->write(Header, 4);
for (int i = 0; i < 128; i++) {
TasmotaClient_Serial->write(data[i]);
uint8_t ProgParams[] = {0x86, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x03, 0xff, 0xff, 0xff, 0xff, 0x00, 0x80, 0x04, 0x00, 0x00, 0x00, 0x80, 0x00};
if (!TasmotaClient_execParam(CMND_STK_SET_DEVICE, ProgParams, sizeof(ProgParams))) {
return 2; // Error: Could not configure device for programming (1)
}
TasmotaClient_Serial->write(CONST_STK_CRC_EOP);
TasmotaClient_waitForSerialData(2, TASMOTA_CLIENT_TIMEOUT);
TasmotaClient_Serial->read();
TasmotaClient_Serial->read();
}

uint32_t TasmotaClient_Flash(uint8_t* data, size_t size) {
if (!TasmotaClient_SetupFlash()) {
// AddLog_P(LOG_LEVEL_INFO, PSTR("TCL: Flashing aborted!"));
return 1; // Error 1: Flashing aborted
uint8_t ExtProgParams[] = {0x05, 0x04, 0xd7, 0xc2, 0x00};
if (!TasmotaClient_execParam(CMND_STK_SET_DEVICE_EXT, ExtProgParams, sizeof(ExtProgParams))) {
return 3; // Error: Could not configure device for programming (2)
}

if (!TasmotaClient_execCmd(CMND_STK_ENTER_PROGMODE)) {
return 4; // Error: Failed to put bootloader into programming mode
}

SHParse.FlashPageIdx = 0;
Expand All @@ -371,38 +348,37 @@ uint32_t TasmotaClient_Flash(uint8_t* data, size_t size) {
uint32_t read = 0;
uint32_t processed = 0;
uint32_t position = 0;

uint32_t error = 0;

while (read < size) {
memcpy(flash_buffer, data + read, sizeof(flash_buffer));
read = read + sizeof(flash_buffer);

// AddLogBuffer(LOG_LEVEL_DEBUG, (uint8_t*)flash_buffer, 32);

read = read + sizeof(flash_buffer);
for (uint32_t ca = 0; ca < sizeof(flash_buffer); ca++) {
processed++;
if ((processed <= size) && (!SHParse.EndOfFile)) {
// flash_buffer = :100000000C945D000C9485000C9485000C94850084<0x0D><0x0A>
if (':' == flash_buffer[ca]) {
position = 0;
}
else if (0xFF == (uint8_t)flash_buffer[ca]) {
// AddLog_P(LOG_LEVEL_DEBUG, PSTR("DBG: Size %d, Processed %d"), size, processed);
// AddLogBuffer(LOG_LEVEL_DEBUG, (uint8_t*)&flash_buffer[ca], 8);
error = 3; // Error 3: Invalid data
break;
}
else if (0x0D == flash_buffer[ca]) {
// 100000000C945D000C9485000C9485000C94850084
thishexline[position] = 0;
error = SimpleHexParseLine(thishexline);
if (error) { break; } // Error 4 and 5
if (error) { break; } // Error 5 and 6
if (SHParse.FlashPageIdx == sizeof(SHParse.FlashPage)) {
TasmotaClient_flashPage(SHParse.ptr_h, SHParse.ptr_l, SHParse.FlashPage);
SHParse.FlashPageIdx = 0;
}
}
else if (0x0A != flash_buffer[ca]) {
if (!isalnum(flash_buffer[ca])) {
// AddLog_P(LOG_LEVEL_DEBUG, PSTR("DBG: Size %d, Processed %d"), size, processed);
error = 7; // Error: Invalid data
break;
}
if (position < sizeof(thishexline) -2) {
thishexline[position++] = flash_buffer[ca];
}
Expand All @@ -413,8 +389,8 @@ uint32_t TasmotaClient_Flash(uint8_t* data, size_t size) {
}

TasmotaClient_exitProgMode();
// AddLog_P(LOG_LEVEL_INFO, PSTR("TCL: Flash done!"));
return error;

return error; // Error or Flash done!
}

void TasmotaClient_Init(void) {
Expand Down

0 comments on commit 9433b69

Please sign in to comment.