Skip to content

Added ESP:: functions for sketch size #2028

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 30 additions & 14 deletions cores/esp32/Esp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,11 @@
#include <memory>
#include <soc/soc.h>
#include <soc/efuse_reg.h>

/* 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 <esp_partition.h>
#include <esp_ota_ops.h>
extern "C" {
#include <esp_image_format.h>
}

/**
* User-defined Literals
Expand Down Expand Up @@ -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::getFreeSketchSpace () {
return sketchSize(SKETCH_SIZE_FREE);
}

uint8_t EspClass::getChipRevision(void)
{
esp_chip_info_t chip_info;
Expand Down
8 changes: 8 additions & 0 deletions cores/esp32/Esp.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -84,6 +89,9 @@ class EspClass
uint32_t magicFlashChipSpeed(uint8_t byte);
FlashMode_t magicFlashChipMode(uint8_t byte);

uint32_t getSketchSize();
uint32_t getFreeSketchSpace();

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);
Expand Down