Skip to main content
deleted 1 character in body
Source Link
Jaromanda X
  • 656
  • 1
  • 6
  • 12

I think you're reading of flash is slightly wrong

while ESP.flashRead takes a unit32_t pointer for output data, the read "count" argument is in bytes - looking at Updater.cpp code it does this

uint8_t buff[128]; for(int i = 0; i < binSize; i += sizeof(buff)) { ESP.flashRead(_startAddress + i, (uint32_t *)buff, sizeof(buff)); ..... } 

so, the the address and count are in bytes - given that SPI_FLASH_SEC_SIZE is 0x1000 or 4096, your code is doing the following

  • reading 4096 bytes into a 16384 buffer
  • writing 16384 bytes (so the last 12288 bytes are zero, or random, not sure)
  • adding 4096 to the address
  • subtracting 16384 from size
  • repeat until all read

So, the written size will be correct, but the data will be three quarters wrong :p

Therefore, your code should be

unsigned long addr = 0; unsigned long rem = ESP.getSketchSize(); uint32_tuint8_t *data = new uint8_t[SPI_FLASH_SEC_SIZE]; int sz = SPI_FLASH_SEC_SIZE; File f = SPIFFS.open("/fw.bin", "w"); while (rem > 0) { ESP.flashRead(addr, (uint32_t *)data, SPI_FLASH_SEC_SIZE); if (rem < sz) sz = rem; f.write(data, sz); addr += SPI_FLASH_SEC_SIZE; rem -= sz; yield(); Serial.println(rem); } f.flush(); f.close(); 

I think you're reading of flash is slightly wrong

while ESP.flashRead takes a unit32_t pointer for output data, the read "count" argument is in bytes - looking at Updater.cpp code it does this

uint8_t buff[128]; for(int i = 0; i < binSize; i += sizeof(buff)) { ESP.flashRead(_startAddress + i, (uint32_t *)buff, sizeof(buff)); ..... } 

so, the the address and count are in bytes - given that SPI_FLASH_SEC_SIZE is 0x1000 or 4096, your code is doing the following

  • reading 4096 bytes into a 16384 buffer
  • writing 16384 bytes (so the last 12288 bytes are zero, or random, not sure)
  • adding 4096 to the address
  • subtracting 16384 from size
  • repeat until all read

So, the written size will be correct, but the data will be three quarters wrong :p

Therefore, your code should be

unsigned long addr = 0; unsigned long rem = ESP.getSketchSize(); uint32_t *data = new uint8_t[SPI_FLASH_SEC_SIZE]; int sz = SPI_FLASH_SEC_SIZE; File f = SPIFFS.open("/fw.bin", "w"); while (rem > 0) { ESP.flashRead(addr, (uint32_t *)data, SPI_FLASH_SEC_SIZE); if (rem < sz) sz = rem; f.write(data, sz); addr += SPI_FLASH_SEC_SIZE; rem -= sz; yield(); Serial.println(rem); } f.flush(); f.close(); 

I think you're reading of flash is slightly wrong

while ESP.flashRead takes a unit32_t pointer for output data, the read "count" argument is in bytes - looking at Updater.cpp code it does this

uint8_t buff[128]; for(int i = 0; i < binSize; i += sizeof(buff)) { ESP.flashRead(_startAddress + i, (uint32_t *)buff, sizeof(buff)); ..... } 

so, the the address and count are in bytes - given that SPI_FLASH_SEC_SIZE is 0x1000 or 4096, your code is doing the following

  • reading 4096 bytes into a 16384 buffer
  • writing 16384 bytes (so the last 12288 bytes are zero, or random, not sure)
  • adding 4096 to the address
  • subtracting 16384 from size
  • repeat until all read

So, the written size will be correct, but the data will be three quarters wrong :p

Therefore, your code should be

unsigned long addr = 0; unsigned long rem = ESP.getSketchSize(); uint8_t *data = new uint8_t[SPI_FLASH_SEC_SIZE]; int sz = SPI_FLASH_SEC_SIZE; File f = SPIFFS.open("/fw.bin", "w"); while (rem > 0) { ESP.flashRead(addr, (uint32_t *)data, SPI_FLASH_SEC_SIZE); if (rem < sz) sz = rem; f.write(data, sz); addr += SPI_FLASH_SEC_SIZE; rem -= sz; yield(); Serial.println(rem); } f.flush(); f.close(); 
added 277 characters in body
Source Link
Jaromanda X
  • 656
  • 1
  • 6
  • 12

I think youryou're reading of flash is slightly wrong

while ESP.flashReadESP.flashRead takes a unit32_t ptrpointer for output data, the read "count" argument is in bytes - looking at Updater.cpp code it does this

uint8_t buff[128]; for(int i = 0; i < binSize; i += sizeof(buff)) { ESP.flashRead(_startAddress + i, (uint32_t *)buff, sizeof(buff)); ..... } 

so, the the address and count are in bytes - given that SPI_FLASH_SEC_SIZE is 0x1000 or 4096, your code is doing the following

  • reading 4096 bytes into a 16384 buffer
  • writing 16384 bytes (so the last 12288 bytes are zero, or random, not sure)
  • adding 4096 to the address
  • subtracting 16384 from size
  • repeat until all read

So, the written size will be correct, but the data will be three quarters wrong :p

Therefore, your code should be

unsigned long addr = 0; unsigned long rem = ESP.getSketchSize(); uint32_t *data = new uint8_t[SPI_FLASH_SEC_SIZE]; int sz = SPI_FLASH_SEC_SIZE; File f = SPIFFS.open("/fw.bin", "w"); while (rem > 0) { ESP.flashRead(addr, (uint32_t *)data, SPI_FLASH_SEC_SIZE); if (rem < sz) sz = rem; f.write(data, sz); addr += SPI_FLASH_SEC_SIZE; rem -= sz; yield(); Serial.println(rem); } f.flush(); f.close(); 

I think your reading of flash is slightly wrong

while ESP.flashRead takes a unit32_t ptr for data, the read "count" argument is in bytes

Therefore, your code should be

unsigned long addr = 0; unsigned long rem = ESP.getSketchSize(); uint32_t *data = new uint8_t[SPI_FLASH_SEC_SIZE]; int sz = SPI_FLASH_SEC_SIZE; File f = SPIFFS.open("/fw.bin", "w"); while (rem > 0) { ESP.flashRead(addr, (uint32_t *)data, SPI_FLASH_SEC_SIZE); if (rem < sz) sz = rem; f.write(data, sz); addr += SPI_FLASH_SEC_SIZE; rem -= sz; yield(); Serial.println(rem); } f.flush(); f.close(); 

I think you're reading of flash is slightly wrong

while ESP.flashRead takes a unit32_t pointer for output data, the read "count" argument is in bytes - looking at Updater.cpp code it does this

uint8_t buff[128]; for(int i = 0; i < binSize; i += sizeof(buff)) { ESP.flashRead(_startAddress + i, (uint32_t *)buff, sizeof(buff)); ..... } 

so, the the address and count are in bytes - given that SPI_FLASH_SEC_SIZE is 0x1000 or 4096, your code is doing the following

  • reading 4096 bytes into a 16384 buffer
  • writing 16384 bytes (so the last 12288 bytes are zero, or random, not sure)
  • adding 4096 to the address
  • subtracting 16384 from size
  • repeat until all read

So, the written size will be correct, but the data will be three quarters wrong :p

Therefore, your code should be

unsigned long addr = 0; unsigned long rem = ESP.getSketchSize(); uint32_t *data = new uint8_t[SPI_FLASH_SEC_SIZE]; int sz = SPI_FLASH_SEC_SIZE; File f = SPIFFS.open("/fw.bin", "w"); while (rem > 0) { ESP.flashRead(addr, (uint32_t *)data, SPI_FLASH_SEC_SIZE); if (rem < sz) sz = rem; f.write(data, sz); addr += SPI_FLASH_SEC_SIZE; rem -= sz; yield(); Serial.println(rem); } f.flush(); f.close(); 
Source Link
Jaromanda X
  • 656
  • 1
  • 6
  • 12

I think your reading of flash is slightly wrong

while ESP.flashRead takes a unit32_t ptr for data, the read "count" argument is in bytes

Therefore, your code should be

unsigned long addr = 0; unsigned long rem = ESP.getSketchSize(); uint32_t *data = new uint8_t[SPI_FLASH_SEC_SIZE]; int sz = SPI_FLASH_SEC_SIZE; File f = SPIFFS.open("/fw.bin", "w"); while (rem > 0) { ESP.flashRead(addr, (uint32_t *)data, SPI_FLASH_SEC_SIZE); if (rem < sz) sz = rem; f.write(data, sz); addr += SPI_FLASH_SEC_SIZE; rem -= sz; yield(); Serial.println(rem); } f.flush(); f.close();