From f9d160e62c7ebd54e028773018ed060eb0ee9c30 Mon Sep 17 00:00:00 2001 From: Larry Bernstone Date: Wed, 7 Nov 2018 10:55:32 -0700 Subject: [PATCH 1/2] Added ESP:: functions for sketch size --- cores/esp32/Esp.cpp | 44 ++++++++++++++++++++++++++++++-------------- cores/esp32/Esp.h | 8 ++++++++ 2 files changed, 38 insertions(+), 14 deletions(-) diff --git a/cores/esp32/Esp.cpp b/cores/esp32/Esp.cpp index 122271be26e..3f09fb574a8 100644 --- a/cores/esp32/Esp.cpp +++ b/cores/esp32/Esp.cpp @@ -25,20 +25,11 @@ #include #include #include - -/* Main header of binary image */ -typedef struct { - uint8_t magic; - uint8_t segment_count; - uint8_t spi_mode; /* flash read mode (esp_image_spi_mode_t as uint8_t) */ - uint8_t spi_speed: 4; /* flash frequency (esp_image_spi_freq_t as uint8_t) */ - uint8_t spi_size: 4; /* flash chip size (esp_image_flash_size_t as uint8_t) */ - uint32_t entry_addr; - uint8_t encrypt_flag; /* encrypt flag */ - uint8_t extra_header[15]; /* ESP32 additional header, unused by second bootloader */ -} esp_image_header_t; - -#define ESP_IMAGE_HEADER_MAGIC 0xE9 +#include +#include +extern "C" { +#include +} /** * User-defined Literals @@ -156,6 +147,31 @@ uint32_t EspClass::getMaxAllocPsram(void) return heap_caps_get_largest_free_block(MALLOC_CAP_SPIRAM); } +static uint32_t sketchSize(sketchSize_t response) { + esp_image_metadata_t data; + const esp_partition_t *running = esp_ota_get_running_partition(); + if (!running) return 0; + const esp_partition_pos_t running_pos = { + .offset = running->address, + .size = running->size, + }; + data.start_addr = running_pos.offset; + esp_image_load(ESP_IMAGE_VERIFY, &running_pos, &data); + if (response) { + return running_pos.size - data.image_len; + } else { + return data.image_len; + } +} + +uint32_t EspClass::getSketchSize () { + return sketchSize(SKETCH_SIZE_TOTAL); +} + +uint32_t EspClass::getFreeSketchSize () { + return sketchSize(SKETCH_SIZE_FREE); +} + uint8_t EspClass::getChipRevision(void) { esp_chip_info_t chip_info; diff --git a/cores/esp32/Esp.h b/cores/esp32/Esp.h index 13b76cd570f..05bafd2ae68 100644 --- a/cores/esp32/Esp.h +++ b/cores/esp32/Esp.h @@ -50,6 +50,11 @@ typedef enum { FM_UNKNOWN = 0xff } FlashMode_t; +typedef enum { + SKETCH_SIZE_TOTAL = 0, + SKETCH_SIZE_FREE = 1 +} sketchSize_t; + class EspClass { public: @@ -84,6 +89,9 @@ class EspClass uint32_t magicFlashChipSpeed(uint8_t byte); FlashMode_t magicFlashChipMode(uint8_t byte); + uint32_t getSketchSize(); + uint32_t getFreeSketchSize(); + bool flashEraseSector(uint32_t sector); bool flashWrite(uint32_t offset, uint32_t *data, size_t size); bool flashRead(uint32_t offset, uint32_t *data, size_t size); From a049477854715bb3f5005f550948c527b2e668f0 Mon Sep 17 00:00:00 2001 From: Larry Bernstone Date: Wed, 7 Nov 2018 11:00:38 -0700 Subject: [PATCH 2/2] Fixed free space name to match ESP8266 --- cores/esp32/Esp.cpp | 2 +- cores/esp32/Esp.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cores/esp32/Esp.cpp b/cores/esp32/Esp.cpp index 3f09fb574a8..713ba7ab66c 100644 --- a/cores/esp32/Esp.cpp +++ b/cores/esp32/Esp.cpp @@ -168,7 +168,7 @@ uint32_t EspClass::getSketchSize () { return sketchSize(SKETCH_SIZE_TOTAL); } -uint32_t EspClass::getFreeSketchSize () { +uint32_t EspClass::getFreeSketchSpace () { return sketchSize(SKETCH_SIZE_FREE); } diff --git a/cores/esp32/Esp.h b/cores/esp32/Esp.h index 05bafd2ae68..385bcbb4b1f 100644 --- a/cores/esp32/Esp.h +++ b/cores/esp32/Esp.h @@ -90,7 +90,7 @@ class EspClass FlashMode_t magicFlashChipMode(uint8_t byte); uint32_t getSketchSize(); - uint32_t getFreeSketchSize(); + uint32_t getFreeSketchSpace(); bool flashEraseSector(uint32_t sector); bool flashWrite(uint32_t offset, uint32_t *data, size_t size);