Skip to content

Commit

Permalink
Perform reset only when there is no work to be done
Browse files Browse the repository at this point in the history
When the application schedules a reset, we need to postpone the reset
until the main loop (thread) has no more work to do to give ongoing
tasks, e.g., NVM update, a chance to finish. The main thread has no more
work to when the application has no sleep lock active.

Fixes a bug introduced by the fix for #53
  • Loading branch information
janakj committed Apr 20, 2022
1 parent 27433f5 commit db80828
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,13 @@ int main(void)
sysconf_process();

CRITICAL_SECTION_BEGIN();
if (schedule_reset) system_reset();

// If the application scheduled a reset, perform it as soon as the MCU
// is allowed to sleep, which indicates that there is no more work to be
// done (e.g., NVM updates).
if (schedule_reset && system_sleep_allowed())
system_reset();

else if (sysconf.sleep) system_low_power();
CRITICAL_SECTION_END();
}
Expand Down
5 changes: 5 additions & 0 deletions src/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ unsigned system_get_stop_mode_mask(void)
return mask;
}

int system_sleep_allowed(void)
{
return sleep_mask == 0;
}

void system_allow_sleep(system_module_t module)
{
irq_disable();
Expand Down
2 changes: 2 additions & 0 deletions src/system.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ bool system_is_stop_mode_allowed(void);

unsigned system_get_stop_mode_mask(void);

int system_sleep_allowed(void);

void system_allow_sleep(system_module_t module);

void system_disallow_sleep(system_module_t module);
Expand Down

0 comments on commit db80828

Please sign in to comment.