Proper sleep implementation in Pycardium
The current sleep in Pycardium (based on mxc_delay()
) is not ideal for a number of reasons:
- It busy-spins for the duration of the delay which wastes energy unnecessarily and does not allow for any sort of sleep.
- It does not run scheduled interrupt handlers. This is most noticeable with button-interrupts as implemented in !297.
- It is not interruptible by CTRL-C.
The best solution to this is probably a re-implementation of the logic behind mxc_delay()
. This means defining a SysTick_Handler
to count overflows and a delay-function which checks the timer value on every IRQ wakeup. In (simplified) code:
void systick_delay(uint32_t usec)
{
/* Setup SysTick */
/* ... */
while (waiting) {
/* Handle scheduled interrupt handlers/callbacks */
mp_handle_pending();
/* Wait for any IRQ, including the SysTick */
__WFI();
}
/* Tear down SysTick */
/* ... */
}
Note that this implementation does not deal with CTRL-C
correctly yet (point 3). I'm not quite sure what is needed for that to work properly.