Functions are running out of order, non-sequentially

I thought all routines and subroutines would run sequentially so I am confused by this behavior.

I am trying to debug my program which has recently started crashing with a “hard fault”. I’ve added a bunch of Serial.println() commands to try to find out where it is crashing. In the setup routine, there are several other initialization routines called that are in other modules. But they seem to be out of order and I wonder what would cause this? Is it an issue with the serial printing? or is there some threading going on that I am not aware of?

In each of my separate initialization routines, I added the Serial.println() at the end so it would print before going back to the main setup routine. I thought the program would step through each routine including all subroutines sequentially.

Here is the setup routine:

void setup() {
    Serial.begin(9600); 
    long nt = millis();
    while (millis() < nt+6000) {}; //****************************88debug
    Serial.println("\n\n");
    Serial.printf("%s\nrevision %s\n", PROG_NAME, REVISION);
    
    // hardware setup
    pinMode(HUB_WAKEUP_PIN, OUTPUT);
    pinMode(BLE_ENABLE_PIN, OUTPUT); digitalWrite(BLE_ENABLE_PIN, LOW);
    //pinMode(BAT_MONITOR_PIN, INPUT); // no need to set pinmode for analog input pins
    pinMode(AC_MONITOR_PIN, INPUT_PULLDOWN);
    enableAllTimers(false);
    enableParticleLED(true);
    enableAllRemotes(true);
    
    // record wakeup time
    logEntry(WAKEUP_BUTTON, logArray, logLength);
    showLogEntries(logArray, logLength); //*****************************************************8 debug
    
    // set up connection with cloud pub/sub
    delay(3000); // ************************************************ debug
    publishInit();
    delay(3000); // ************************************************ debug
    pubStackInit();
    delay(3000); // ************************************************ debug

    // main hub power functions
    powerCheckTimer.start();
    // check power immediately
    processPowerCheck = true;
    pluginState = checkACPlugin();
    for (int i = 0; i < MAIN_BAT_AVE_COUNT; i++) {
        mainBatAveVolt[i] = 1240;       // nomial default for battery voltage
    }

    // communication with wifi hub (before cloud connect in case it has to be shut down due to low battery power)
    remoteCommInit();
    
    // connect to the cloud
    if (!Particle.connected()) Particle.connect();

My serial output looks like this:

Airstream-ctrl-hub
revision v.8.0.0
--remoteCommInit done
GPS enabled
--pubStackInit done
Serial connection closed.
Caught Interrupt.  Cleaning up.

So it looks like the order is title, remoteCommInit(), GPSInit(), PubStackInit(). But in the setup you can see a different order. The title and revision is first which is expected. But then it should be publishInit() (which doesn’t even get printed), then pubStackinit(), then further down remoteCommInit(). GPS initialization is about 20 lines after the end of what I copied.

PubStackInit() for instance looks like this but all the initialization routines are similarly short

/* initialization */
void pubStackInit() {
    cloudPublishTimer.start();    
    Serial.println("--pubStackInit done"); //********************************************* debug
}
  1. What am I missing that would cause the routines to run in a different order (and possibly point me to the real crashing problem)?
  2. Is there a better way to find where the fault is coming from?

This is running on an Electron with v.0.8.0-rc.8.

There isn’t enough of your code to actually tell, but

  • serial output is written to a buffer which will be sent in the background via the HW interface independently of your code
  • Software Timers are running on a dedicated timer thread - again independently of your main loop
  • global constructors are executed in a non-deterministic order
  • if you are running SYSTEM_THREAD(ENABLED) the system tasks are executed on the independent system thread
1 Like