This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Is it bad to exit the main 'for(;;) loop when application is done?

The application I am designing gets a pulse ox measurement from the user, sends it, disconnects, and shuts off. Are there any deleterious side effects of exiting the main for(;;) loop present in almost all of the examples?  I want to disconnect from the peer, disable soft device, write data to flash if needed, and exit. The disabling of soft device will cause a fatal error when sd_app_evt_wait() is called so I definitely do not want to make that call and I don't want to skip it either as the loop will just burn battery calories. There is no reason to re-initialize SoftDevice as the device will not run again.

So can I do something like this with no unforeseen consequences in the main loop?

        sd_softdevice_is_enabled(&enabled); // Don't do this if disabled, for example when writing flash at the end
        if (enabled != 1)
        {
            #if (USE_DK == 1)
            NRF_LOG_DEBUG("Exiting application%u\r\n");
            #endif
            while(NRF_LOG_PROCESS());
            return;
        }

I suppose I should add what do I need to clean up if I should do this?

Parents
  • In a baremetal application (application without any RTOS), the PC always needs to have the next jump instruction. That is the reason that in the baremetal application the main thread cannot exit, because that would cause the PC register to increment even after the application end address has reached causeing the CPU to fault eventually or have undefined behavior.

  • So after disabling the softdevice I need to re-enable it? (Its kind of a pain). I disable softdevice because it is so much easier to write to flash - it is linear code and no events.

    I just went for a NVIC_SystemReset(); instead. It put the code back in a loop until the juice is turned off.

  • The disabling of soft device will cause a fatal error when sd_app_evt_wait()

     It seems to me that you want to go to low power mode after the softdevice is disabled. You cannot use sd_app_evt_wait as that needs the softdevice to be enabled.

    instead use

    for(;;) { __SEV(); __WFE(); __WFE() }

    The above will make the system go to low power mode (System IDLE), it is upto your application how you wake it up or if you do not want to wake it up al all.

    If you want to go to even the lowest power you can just set the SystemOFF power register to 1.

        // Set nRF5 into System OFF. Reading out value and looping after setting the register
        // to guarantee System OFF in nRF52.
        NRF_POWER->SYSTEMOFF = 0x1;
        (void) NRF_POWER->SYSTEMOFF;
        while (true);

Reply
  • The disabling of soft device will cause a fatal error when sd_app_evt_wait()

     It seems to me that you want to go to low power mode after the softdevice is disabled. You cannot use sd_app_evt_wait as that needs the softdevice to be enabled.

    instead use

    for(;;) { __SEV(); __WFE(); __WFE() }

    The above will make the system go to low power mode (System IDLE), it is upto your application how you wake it up or if you do not want to wake it up al all.

    If you want to go to even the lowest power you can just set the SystemOFF power register to 1.

        // Set nRF5 into System OFF. Reading out value and looping after setting the register
        // to guarantee System OFF in nRF52.
        NRF_POWER->SYSTEMOFF = 0x1;
        (void) NRF_POWER->SYSTEMOFF;
        while (true);

Children
Related