I am trying to implement firmware updates by sd card.
I’ve formatted a 32GB SD card to have a single FAT32 4GB partion and copied the firmware.bin
from: .pio/build/m5stack-core-esp32
to the card and renamed it to update.bin
. Then i’ve flashed the M5 by USB with the attached code.
On boot with inserted SD Card i get the following output (on the lcd):
SD card found, try update
Could not load update.bin from root
maybe someone has done something similar and could help me?
I am a beginner in ESP32 programming and therefore possible that this is an obvious and simple error on my side
#include <M5Stack.h>
#include <Update.h>
void performUpdate(Stream &updateSource, size_t updateSize) {
if (Update.begin(updateSize)) {
size_t written = Update.writeStream(updateSource);
if (written == updateSize) {
M5.Lcd.println("Written : " + String(written) + " successfully");
}
else {
M5.Lcd.println("Written only : " + String(written) + "/" + String(updateSize) + ". Retry?");
}
if (Update.end()) {
M5.Lcd.println("OTA done!");
if (Update.isFinished()) {
M5.Lcd.println("Update successfully completed. Rebooting.");
}
else {
M5.Lcd.println("Update not finished? Something went wrong!");
}
}
else {
M5.Lcd.println("Error Occurred. Error #: " + String(Update.getError()));
}
}
else
{
M5.Lcd.println("Not enough space to begin OTA");
}
}
void updateFromFS(fs::FS &fs) {
File updateBin = fs.open("/update.bin");
if (updateBin) {
if(updateBin.isDirectory()){
M5.Lcd.println("Error, update.bin is not a file");
updateBin.close();
return;
}
size_t updateSize = updateBin.size();
if (updateSize > 0) {
M5.Lcd.println("Try to start update");
performUpdate(updateBin, updateSize);
}
else {
M5.Lcd.println("Error, file is empty");
}
updateBin.close();
// whe finished remove the binary from sd card to indicate end of the process
fs.remove("/update.bin");
}
else {
M5.Lcd.println("Could not load update.bin from sd root");
}
}
void setup()
{
M5.begin();
if (!SD.begin())
{
M5.Lcd.println("No SD Card");
} else {
M5.Lcd.println("SD Card found, try update");
updateFromFS(SD);
}
M5.Lcd.println("Yay, SD Update worked!");
}
void loop()
{
}