Skip to content

Commit af79e18

Browse files
lbernstoneme-no-dev
authored andcommitted
Added ESP:: functions for sketch size (espressif#2028)
* Added ESP:: functions for sketch size * Fixed free space name to match ESP8266
1 parent 273196d commit af79e18

File tree

2 files changed

+38
-14
lines changed

2 files changed

+38
-14
lines changed

cores/esp32/Esp.cpp

+30-14
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,11 @@
2525
#include <memory>
2626
#include <soc/soc.h>
2727
#include <soc/efuse_reg.h>
28-
29-
/* Main header of binary image */
30-
typedef struct {
31-
uint8_t magic;
32-
uint8_t segment_count;
33-
uint8_t spi_mode; /* flash read mode (esp_image_spi_mode_t as uint8_t) */
34-
uint8_t spi_speed: 4; /* flash frequency (esp_image_spi_freq_t as uint8_t) */
35-
uint8_t spi_size: 4; /* flash chip size (esp_image_flash_size_t as uint8_t) */
36-
uint32_t entry_addr;
37-
uint8_t encrypt_flag; /* encrypt flag */
38-
uint8_t extra_header[15]; /* ESP32 additional header, unused by second bootloader */
39-
} esp_image_header_t;
40-
41-
#define ESP_IMAGE_HEADER_MAGIC 0xE9
28+
#include <esp_partition.h>
29+
#include <esp_ota_ops.h>
30+
extern "C" {
31+
#include <esp_image_format.h>
32+
}
4233

4334
/**
4435
* User-defined Literals
@@ -156,6 +147,31 @@ uint32_t EspClass::getMaxAllocPsram(void)
156147
return heap_caps_get_largest_free_block(MALLOC_CAP_SPIRAM);
157148
}
158149

150+
static uint32_t sketchSize(sketchSize_t response) {
151+
esp_image_metadata_t data;
152+
const esp_partition_t *running = esp_ota_get_running_partition();
153+
if (!running) return 0;
154+
const esp_partition_pos_t running_pos = {
155+
.offset = running->address,
156+
.size = running->size,
157+
};
158+
data.start_addr = running_pos.offset;
159+
esp_image_load(ESP_IMAGE_VERIFY, &running_pos, &data);
160+
if (response) {
161+
return running_pos.size - data.image_len;
162+
} else {
163+
return data.image_len;
164+
}
165+
}
166+
167+
uint32_t EspClass::getSketchSize () {
168+
return sketchSize(SKETCH_SIZE_TOTAL);
169+
}
170+
171+
uint32_t EspClass::getFreeSketchSpace () {
172+
return sketchSize(SKETCH_SIZE_FREE);
173+
}
174+
159175
uint8_t EspClass::getChipRevision(void)
160176
{
161177
esp_chip_info_t chip_info;

cores/esp32/Esp.h

+8
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ typedef enum {
5050
FM_UNKNOWN = 0xff
5151
} FlashMode_t;
5252

53+
typedef enum {
54+
SKETCH_SIZE_TOTAL = 0,
55+
SKETCH_SIZE_FREE = 1
56+
} sketchSize_t;
57+
5358
class EspClass
5459
{
5560
public:
@@ -84,6 +89,9 @@ class EspClass
8489
uint32_t magicFlashChipSpeed(uint8_t byte);
8590
FlashMode_t magicFlashChipMode(uint8_t byte);
8691

92+
uint32_t getSketchSize();
93+
uint32_t getFreeSketchSpace();
94+
8795
bool flashEraseSector(uint32_t sector);
8896
bool flashWrite(uint32_t offset, uint32_t *data, size_t size);
8997
bool flashRead(uint32_t offset, uint32_t *data, size_t size);

0 commit comments

Comments
 (0)