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

Waking when app_sched_event_put() is called from main context

Most application examples which use the scheduler have a main loop looking something like this,

int main(void)
{
    // Initialization <snipped> 

    // Enter main loop.
    for (;;)
    {
    	app_sched_execute();
        if (NRF_LOG_PROCESS() == false)
        {
            ret_code_t err_code = sd_app_evt_wait();  // or __WFE() if no soft device is used
            APP_ERROR_CHECK(err_code);
        }
    }
}

Clearly if an interrupt routine pushes an event using app_sched_event_put() then the application will wake from sd_app_event_wait() and the event handler will be executed when the main loop calls app_sched_execute().

What happens though if the main loop itself (or an event handler executing in the main context via an app_sched_execute() call) pushes an event to the queue but no subsequent interrupt occurs? Does the application then remain asleep with the pushed event stuck in the queue?

I assume this is the case, but just wanted to confirm, as it suggests perhaps using a timer to periodically schedule execution of a "master" application event hander / state machine which then processes events (e.g., bit-flags) set by the various app_sched_execute()'ed handlers.

Thanks!

Parents
  • Hi,

    Here is what the SDK team had to say about the issue:

    This is the body of app_sched_execute:

    void app_sched_execute(void)
    {
        while (!is_app_sched_paused() && !APP_SCHED_QUEUE_EMPTY())
        {
            ...
        }
    }
    

    So as long as the scheduler has things to process and isn't paused, then the function will not exit. And if the scheduler is paused it has to be started from the interrupt anyway. When there are no more events to process there is no chance for the main thread to add anything - we are waiting for an interrupt anyway.

  • Hi Martin,

    That makes perfect sense. It is therefore completely safe to push events from any event hander, whether running in interrupt context or main context. It would however be "bad" to push an event from the main loop directly between the calls to app_sched_execute() and sd_app_event_wait().

    Thanks for the quick reply!

    Don

Reply Children
No Data
Related