This commit adds an implementation of a "software timer" with a 1ms
resolution, using SysTick. It allows unlimited number of concurrent
timers (limited only by memory needed for each timer entry). They can be
one-shot or periodic, and associated with a Python callback.
There is a very small overhead added to the SysTick IRQ, which could be
further optimised in the future, eg by patching SysTick_Handler code
dynamically.
Instead of checking each callback (currently storage and dma) explicitly
for each SysTick IRQ, use a simple circular function table indexed by the
lower bits of the millisecond tick counter. This allows callbacks to be
easily enabled/disabled at runtime, and scales well to a large number of
callbacks.
This is to keep the top-level directory clean, to make it clear what is
core and what is a port, and to allow the repository to grow with new ports
in a sustainable way.
ExtInt, Timer and CAN IRQ callbacks are made to work with the scheduler.
They are still hard IRQs by default, but one can now call
micropython.schedule within the hard IRQ to schedule a soft callback.
The renames are:
HAL_Delay -> mp_hal_delay_ms
sys_tick_udelay -> mp_hal_delay_us
sys_tick_get_microseconds -> mp_hal_ticks_us
And mp_hal_ticks_ms is added to provide the full set of timing functions.
Also, a separate HAL_Delay function is added which differs slightly from
mp_hal_delay_ms and is intended for use only by the ST HAL functions.
This patch changes the threading implementation from simple round-robin
with busy waits on mutexs, to proper scheduling whereby threads that are
waiting on a mutex are only scheduled when the mutex becomes available.
pyb.delay and pyb.udelay now use systick if IRQs are enabled, otherwise
they use a busy loop. Thus they work correctly when IRQs are disabled.
The busy loop is computed from the current CPU frequency, so works no
matter the CPU frequency.
SysTick IRQ now increases millisecond counter directly (ie without
calling HAL_IncTick). Provide our own version of HAL_Delay that does a
wfi while waiting. This more than halves power consumption when running
a loop containing a pyb.delay call. It used to be like this, but new
version of HAL library regressed this feature.
I also removed trailing spaces from modpyb.c which affected a couple
of lines technically not part of this patch.
Tested using: https://github.com/dhylands/upy-examples/blob/master/micros_test.py
which eventually fails due to wraparound issues (I could fix the test to compensate
but didn't bother)
Blanket wide to all .c and .h files. Some files originating from ST are
difficult to deal with (license wise) so it was left out of those.
Also merged modpyb.h, modos.h, modstm.h and modtime.h in stmhal/.