Run portion of code before flashing new code?

Hi Particle Community,
I was wondering, is it possible to have the Photon run a particular bit of code when it senses new code being flashed, before it actually flashes?

Specifically, I’m wondering if I can do the following:
-Compile my code from the desktop IDE.
-This action triggers the Photon to write voltages to several pins in a particular order.
-AFTER those voltages are written, the Photon updates to the newly compiled code.

An analogy in pseudo-code form:

if(NewCodeAboutToBeFlashed)
{
digitalWrite(1,HIGH);
delay(100);
digitalWrite(1,LOW);
delay(100);

FlashNewCode();
}

Not really a troubleshooting topic, since I haven’t done it yet our seen it done, but it’d be a cool feature to have access to for my project!
Thanks in advance!

I think you could check System.updatesPending() with a flag to see if the photon has started an OTA update.

You would probably need to have SYSTEM_THREAD(ENABLED);

https://docs.particle.io/reference/device-os/firmware/photon/#system-updatespending-

In loop() you could have the following block:

if (System.updatesPending() && !otaStarted) {
    otaStarted = true;
    digitalWrite(1,HIGH);
    delay(100);
    digitalWrite(1,LOW);
    delay(100);
}

You might be also be able to listen for a system event and use a handler:
https://docs.particle.io/reference/device-os/firmware/photon/#system-events-reference

To do this you could do something like this:

void otaHandler(system_event_t event, int param) {
    if (param == firmware_update_begin) {
        digitalWrite(1,HIGH);
        delay(100);
        digitalWrite(1,LOW);
        delay(100);
    }
}

void setup() {
    // listen for firmware update events
    System.on(firmware_update, otaHandler);
}
1 Like

Gotcha, I was just going to ask if that was the syntax to use for that 2nd suggestion.

However, the event naming has me a bit concerned: if the event is called “firmware_update_complete,” will it actually run the code before flashing? Via naming it seems like it would only run the handler after the flashing was done.

You could try firmware_update_begin perhaps. I think it would call the handler when the flash begins.

I’ll try one of these momentarily. My other concern is that even if the handler is called, it may be interrupted by the flash.

Thoughts on this, so that the update can’t go through till the code has run?

void otaHandler(system_event_t event, int param) {
if (param == firmware_update_pending) {
digitalWrite(1,HIGH);
delay(100);
digitalWrite(1,LOW);
delay(100);
System.enableUpdates();
}
}

void setup() {
System.disableUpdates();
System.on(firmware_update, otaHandler);
}

I doubt it would be interrupted. You could also try running the code in a handler listening for the reset event, and the code would be run before the photon resets.

@bjjesteadt i did something similar last time to ensure that my plant does not end up overflowing water when an OTA is happening and my pump is running! :smiley:

Something like:

This implementation runs the otaHandler() function before taking in the OTA update.

You might want to instead use firmware_update_complete which should (i never tested) call the function when OTA completed (but not yet rebooting)

3 Likes