This is not my code. It's an example I found online and it was very useful to test the firmware upgrade functions of the particle photon: https://community.particle.io/t/how-to-get-access-to-firmware-flashing-functions/19103/14
Important things to know: You can use the bin file generated by using the CLI command: particle compile photon c:\myapp
FileTransfer::Descriptor file; file.file_length // exact size of the bin file. file.file_address = 0; // Automatically set to HAL_OTA_FlashAddress if store is FIRMWARE file.chunk_address = 0; file.chunk_size = 512; You must remember that once this is set to a number (or 0 to use default values), you can't send a chunk bigger than this to the Save functino.
If you pass 1 on second parameter of the Prepare function, it's only to validate the firmware so you need to pass 0 to be able to upgrade the device. I also found that if I call this while I'm in the page handler of the SoftAP, it will hang here.
Spark_Prepare_For_Firmware_Update(file, 0, NULL);
THIS IS IMPORTANT. You must do that after calling the prepare function: // Note that Spark_Prepare_For_Firmware_Update sets file.file_address so it's not really zero here // even though it's what we initialize it to above! file.chunk_address = file.file_address;
After each call of the save function, you must increase the chuck_address. Spark_Save_Firmware_Chunk(file, &tinker[offset], NULL); file.chunk_address += file.chunk_size;
The Finish function will automatically reboot the device so the code after this call wont be executed except if upgrade failed Spark_Finish_Firmware_Update(file, 0x1, NULL);
To help debugging this, I recommand to use the System Events. To do that, add this fucntion to your application https://docs.particle.io/reference/firmware/photon/#system-events
void handle_all_the_events(system_event_t event, int param) { Serial.printlnf("Got event %d with value %d", ev, param);
if (ev == 1024)
System.enableReset(); // allow reset just in case.
if (ev == 512)
System.enableUpdates(); // Allow updates just in case
}
void setup() { System.on(all_events, handle_all_the_events); }