MCPcopy Create free account
hub / github.com/OpenEPaperLink/OpenEPaperLink / flash_binary

Function flash_binary

ESP32_AP-Flasher/src/espflasher.cpp:53–127  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

51#define MIN(a, b) ((a) < (b) ? (a) : (b))
52
53esp_loader_error_t flash_binary(String &file_path, size_t address) {
54 esp_loader_error_t err;
55
56 wsSerial("Flashing " + String(file_path));
57
58 File file = contentFS->open(file_path, "rb");
59 if (!file) {
60 wsSerial("Failed to open file");
61 return ESP_LOADER_ERROR_FAIL;
62 }
63
64 size_t size = file.size();
65 static uint8_t payload[1024];
66 Serial.println("file size: " + String(size));
67
68 printf("Erasing flash (this may take a while)...\n");
69 err = esp_loader_flash_start(address, size, sizeof(payload));
70 if (err != ESP_LOADER_SUCCESS) {
71 wsSerial("Erasing flash failed");
72 file.close();
73 return err;
74 }
75 printf("Start programming\n");
76
77 size_t written = 0;
78 size_t sizeleft = size;
79 uint32_t t = millis();
80 while (sizeleft > 0) {
81 size_t to_read = MIN(sizeleft, sizeof(payload));
82 size_t bytes_read = file.readBytes(reinterpret_cast<char *>(payload), to_read);
83
84 if (bytes_read != to_read) {
85 wsSerial("Failed to read file.");
86 file.close();
87 return ESP_LOADER_ERROR_FAIL;
88 }
89
90 err = esp_loader_flash_write(payload, to_read);
91 if (err != ESP_LOADER_SUCCESS) {
92 wsSerial("Packet could not be written!");
93 file.close();
94 return err;
95 }
96
97 sizeleft -= to_read;
98 written += to_read;
99
100 if (millis() - t > 250) {
101 uint32_t progress = written * 100 / size;
102 // printf("\rProgress: %d%%", progress);
103 wsSerial("Progress: " + String(progress) + "%");
104 fflush(stdout);
105 t = millis();
106 }
107 };
108 wsSerial("Progress: 100%");
109
110 file.close();

Callers 1

FlashC6_H2Function · 0.70

Calls 8

wsSerialFunction · 0.85
openMethod · 0.80
printlnMethod · 0.80
closeMethod · 0.80
esp_loader_flash_startFunction · 0.50
esp_loader_flash_writeFunction · 0.50
esp_loader_flash_verifyFunction · 0.50
sizeMethod · 0.45

Tested by

no test coverage detected